I have no special talent. I'm only passionately curious - Albert Einstein
April 29, 2010
Running a Jar file with Ant
Posted by Dave Malone
in Configuration,
Java,
Technology
I recently attempted to run a jar file that has multiple third party dependancies from an ant build script. I assumed that you could use a path element to define a path containing all of the external dependancies, and that you could use the built in java task in ant, pointing to my jar file, using the classpathref attribute to run the application. I quickly discovered that I was wrong, and there wasn't a very straightforward explanation on the web (that I could find) as to why.
Here is my working solution for Ant 1.7.1:
<path id="classpath">
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<target name="execute">
<echo message="${jar.file.location}" />
<java classname="${main.class}" fork="true">
<classpath>
<path refid="classpath"/>
<path location="${jar.file.location}"/>
</classpath>
</java>
</target>
Essentially, if you use the java jar="${jar.file.location}", and attempt to pass in the path refid into the java classpath, the jars will be ignored, and you will receive a ClassNotFoundException. Instead, specify the classname for the Java main class you're attempting to run, and add the jar file that contains the class into the classpath. This is the only configuration that worked for me