title: How to include external libraries in Ant build.xml file?
body: <p>In my Java source-code I want to use different classes from java archives (.jar) stored in my application's "lib" directory. But if I do "ant run" then I always get a "java.lang.NoClassDefFoundError" message. I tried several things to fix it but nothing worked... Maybe someone here can help me?</p>
  
  <p>This is my <strong>build.properties</strong> file:</p>
  
  <pre><code>app.name=MyApplication
  app.version=1.0
  main.class=mypackage.MyMain
  build.dir=build
  classes.dir=${build.dir}/classes
  jar.dir=${build.dir}/jar
  dist.dir=dist
  src.dir=src
  test.dir=test
  lib.dir=lib
  </code></pre>
  
  <p>This is my <strong>build.xml</strong>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;project name="My Project" default="run" basedir="."&gt;
    &lt;description&gt;My description.&lt;/description&gt;
  
    &lt;property file="build.properties" /&gt;
    &lt;path id="classpath"&gt;
      &lt;fileset dir="${lib.dir}" includes="*.jar"/&gt;
    &lt;/path&gt;
  
      &lt;!-- Initialization --&gt;
    &lt;target name="init" description="Prepare needed directories."&gt;
      &lt;mkdir dir="${build.dir}" /&gt;
      &lt;mkdir dir="${classes.dir}" /&gt;
      &lt;mkdir dir="${jar.dir}" /&gt;
      &lt;mkdir dir="${dist.dir}" /&gt;
      &lt;mkdir dir="${lib.dir}" /&gt;
    &lt;/target&gt;
  
      &lt;!-- Cleanup --&gt;
    &lt;target name="clean" description="Remove all files created by the build/test process."&gt;
      &lt;delete dir="${classes.dir}" /&gt;
      &lt;delete dir="${dist.dir}" /&gt;
    &lt;/target&gt;
  
      &lt;!-- Compile application --&gt;
    &lt;target name="compile"&gt;
      &lt;mkdir dir="${classes.dir}"/&gt;
      &lt;javac srcdir="${src.dir}" 
         destdir="${classes.dir}" 
         debug="yes"
         includeantruntime="false"&gt;
        &lt;!-- &lt;classpath refid="classpath" /&gt; --&gt;
      &lt;/javac&gt;
    &lt;/target&gt;
  
      &lt;!-- Java Archive --&gt;
    &lt;target name="jar" depends="compile"&gt;
      &lt;!--&lt;delete file="${jar.dir}/${app.name}-${app.version}.jar"/&gt;--&gt;
      &lt;delete dir="${jar.dir}"/&gt;
      &lt;mkdir dir="${jar.dir}"/&gt;
      &lt;jar destfile="${jar.dir}/${app.name}-${app.version}.jar" basedir="${classes.dir}"&gt;
        &lt;manifest&gt;
          &lt;attribute name="Class-Path" value="${lib.dir}"/&gt;
          &lt;attribute name="Main-Class" value="${main.class}"/&gt;
        &lt;/manifest&gt;
      &lt;/jar&gt;
    &lt;/target&gt;
  
      &lt;!-- Run application --&gt;
    &lt;target name="run" depends="jar"&gt;
  
      &lt;java jar="${jar.dir}/${app.name}-${app.version}.jar" 
          fork="true"&gt;
      &lt;/java&gt;
      &lt;!--
      &lt;java fork="true" classname="${main.class}"&gt;
        &lt;classpath&gt;
           &lt;path refid="classpath"/&gt;
           &lt;path location="${jar.dir}/${app.name}-${app.version}.jar"/&gt;
        &lt;/classpath&gt;
      &lt;/java&gt;
      --&gt;
    &lt;/target&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>It would be nice if anyone could help.</p>
  
  <p>Cheers!</p>
  
  <p>Benny</p>