title: How to set the classpath in an ant file to execute a Java program and include external jars?
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;project name="TEDI" basedir="." default="execute"&gt;
      &lt;property name="src" value="src"/&gt;
      &lt;property name="output" value="build"/&gt;
      &lt;property name="lib" value="jung2-2_0_1"/&gt;
  
      &lt;target name="execute" depends="compile"&gt;
          &lt;echo&gt;
              Executing TEDI.
          &lt;/echo&gt;
          &lt;java classname="${output}/TEDI.class"&gt;
              &lt;classpath refid="java"/&gt;
          &lt;/java&gt;
      &lt;/target&gt;
  
      &lt;target name="compile" depends="create"&gt;
          &lt;echo&gt;
              Compiling source files.
          &lt;/echo&gt;
          &lt;javac destdir="${output}"&gt;            
              &lt;src path="${src}"/&gt;            
              &lt;classpath refid="java"/&gt;
          &lt;/javac&gt;
      &lt;/target&gt;
  
      &lt;target name="clean"&gt;
          &lt;echo&gt;
              Deleting old class files.
          &lt;/echo&gt;
          &lt;delete dir="${output}"/&gt;
      &lt;/target&gt;
  
      &lt;target name="create" depends="clean"&gt;
          &lt;echo&gt;
              Creating output directory.
          &lt;/echo&gt;
          &lt;mkdir dir="${output}"/&gt;
      &lt;/target&gt;
  
      &lt;path id="java"&gt;
          &lt;pathelement location="${output}"/&gt;
          &lt;fileset dir="${lib}"&gt;
              &lt;include name="*.jar"/&gt;
          &lt;/fileset&gt;
      &lt;/path&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>When I run ant, it does the clean, create, and compile targets just fine, and then when it gets to execute it says: <code>Could not find build/TEDI.class. Make sure you have it in your classpath</code></p>
  
  <p>Anyway I'm hoping someone can tell me what I'm doing wrong in the <code>path</code> section.  I added the <code>pathelement</code> 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.</p>
  
  <p>Edit: Changed <code>&lt;pathelement location="${build}"/&gt;</code> to <code>&lt;pathelement location="${output}"/&gt;</code> as per Sandro's answer, but it doesn't change the error message at all.</p>