I have no special talent. I'm only passionately curious - Albert Einstein
Running a Jar file with Ant Comment on Running a Jar file with Ant 0

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

 

 


0 comments

Add a comment

Please provide your name, email address (won't be published) and a comment

About

David Malone is a Java developer residing in the Twin Cities area.  He has been developing enterprise applications since 2004.  This is his personal blog, as well as his design and development workspace.