Maltiriel
Maltiriel

Reputation: 793

How to set the classpath in an ant file to execute a Java program and include external jars?

I've found examples on how to either set the classpath so that a Java program can be executed or to include external jars, but not both. My attempts to combine multiple examples in one file have failed.

My source .java files are in the src directory, the file I want to execute is src/TEDI.java, the class files get put correctly into the build directory, and all my jars are in the directory jung2-2_0_1. I can get my program to compile, but not execute, using the following ant file:

<?xml version="1.0"?>
<project name="TEDI" basedir="." default="execute">
    <property name="src" value="src"/>
    <property name="output" value="build"/>
    <property name="lib" value="jung2-2_0_1"/>

    <target name="execute" depends="compile">
        <echo>
            Executing TEDI.
        </echo>
        <java classname="${output}/TEDI.class">
            <classpath refid="java"/>
        </java>
    </target>

    <target name="compile" depends="create">
        <echo>
            Compiling source files.
        </echo>
        <javac destdir="${output}">            
            <src path="${src}"/>            
            <classpath refid="java"/>
        </javac>
    </target>

    <target name="clean">
        <echo>
            Deleting old class files.
        </echo>
        <delete dir="${output}"/>
    </target>

    <target name="create" depends="clean">
        <echo>
            Creating output directory.
        </echo>
        <mkdir dir="${output}"/>
    </target>

    <path id="java">
        <pathelement location="${output}"/>
        <fileset dir="${lib}">
            <include name="*.jar"/>
        </fileset>
    </path>
</project>

When I run ant, it does the clean, create, and compile targets just fine, and then when it gets to execute it says: Could not find build/TEDI.class. Make sure you have it in your classpath

Anyway I'm hoping someone can tell me what I'm doing wrong in the path section. I added the pathelement bit after reading one example on how to create an ant target to execute a file, but it didn't help at all. There are a ton of examples and a lot of them do things differently to achieve the same thing (while none do exactly what I'm trying to do), so I can't figure out which way is correct for what I'm trying to do. Any help or ideas would be greatly appreciated.

Edit: Changed <pathelement location="${build}"/> to <pathelement location="${output}"/> as per Sandro's answer, but it doesn't change the error message at all.

Upvotes: 1

Views: 13683

Answers (2)

Dan Berindei
Dan Berindei

Reputation: 7204

The java task expects a Java class name, not the path of a class file. So you should use

    <java classname="TEDI">

instead of

    <java classname="${output}/TEDI.class">

Upvotes: 1

Sandro
Sandro

Reputation: 1276

As the class you want to execute seems to be in ${output}, you have to include ${output} in your classpath.

Try adding <pathelement location="${output}"/> to your path.

Upvotes: 0

Related Questions