title: Ant + JUnit = ClassNotFoundExceptions when running tests?
body: <p>I'm trying to run some tests in Ant presently using JUnit, and all of my tests are failing with the following stacktrace:</p>
  <pre><code>java.lang.ClassNotFoundException: com.mypackage.MyTestCase
  </code></pre>
  <p>It doesn't make too much sense to me. I'm first compiling my test cases using <code>&lt;javac&gt;</code>, then directly running the <code>&lt;junit&gt;</code> task to run the tests. My buildfile looks like this:</p>
  <pre><code>&lt;target name=&quot;compile.webapp.tests&quot; depends=&quot;compile.webapp&quot;&gt;
      &lt;javac srcdir=&quot;${test.java.src.dir}&quot;
              destdir=&quot;${test.java.bin.dir}&quot;&gt;
          &lt;classpath&gt;
              &lt;filelist&gt;
                  &lt;file name=&quot;${red5.home}/red5.jar&quot;/&gt;
                  &lt;file name=&quot;${red5.home}/boot.jar&quot;/&gt;
                  &lt;file name=&quot;${bin.dir}/${ant.project.name}.jar&quot;/&gt;
              &lt;/filelist&gt;
              &lt;fileset dir=&quot;${red5.lib.dir}&quot; includes=&quot;**/*&quot;/&gt;
              &lt;fileset dir=&quot;${main.java.lib.dir}&quot; includes=&quot;**/*&quot;/&gt;
              &lt;fileset dir=&quot;${test.java.lib.dir}&quot; includes=&quot;**/*&quot;/&gt;
          &lt;/classpath&gt;
      &lt;/javac&gt;
  &lt;/target&gt;
  
  &lt;target name=&quot;run.webapp.tests&quot; depends=&quot;compile.webapp.tests&quot;&gt;
      &lt;junit printsummary=&quot;true&quot;&gt;
          &lt;classpath&gt;
              &lt;filelist&gt;
                  &lt;file name=&quot;${red5.home}/red5.jar&quot;/&gt;
                  &lt;file name=&quot;${red5.home}/boot.jar&quot;/&gt;
                  &lt;file name=&quot;${bin.dir}/${ant.project.name}.jar&quot;/&gt;
              &lt;/filelist&gt;
              &lt;fileset dir=&quot;${red5.lib.dir}&quot; includes=&quot;**/*.jar&quot;/&gt;
              &lt;fileset dir=&quot;${main.java.lib.dir}&quot; includes=&quot;**/*.jar&quot;/&gt;
              &lt;fileset dir=&quot;${test.java.lib.dir}&quot; includes=&quot;**/*.jar&quot;/&gt;
              &lt;fileset dir=&quot;${test.java.bin.dir}&quot; includes=&quot;**/*.class&quot;/&gt;
          &lt;/classpath&gt;
          
          &lt;formatter type=&quot;xml&quot;/&gt;
          
          &lt;batchtest todir=&quot;${test.java.output.dir}&quot;&gt;
              &lt;fileset dir=&quot;${test.java.bin.dir}&quot; includes=&quot;**/*TestCase*&quot;/&gt;
          &lt;/batchtest&gt;
      &lt;/junit&gt;
      
      &lt;junitreport&gt;
          &lt;fileset dir=&quot;${test.java.output.dir}&quot; includes=&quot;**/*&quot;/&gt;
          &lt;report todir=&quot;${test.java.report.dir}&quot;/&gt;
      &lt;/junitreport&gt;
  &lt;/target&gt;
  </code></pre>
  <p>This is really weird, I can't seem to fix this. Is there something I'm doing wrong here?
  My project's directory layout looks somewhat like this:</p>
  <pre><code>${basedir}/src/test/java # this is &quot;test.java.src.dir&quot;
  ${basedir}/build/test/java # this is &quot;test.java.bin.dir&quot;
  ${basedir}/lib/main/java # this is &quot;main.java.lib.dir&quot;
  ${basedir}/lib/test/java # this is &quot;test.java.lib.dir&quot;
  ${basedir}/build/test/junit # this is &quot;test.java.output.dir&quot;
  </code></pre>
  <p>My complete buildfile is available here: <a href="http://pastebin.com/SVnciGKR" rel="nofollow noreferrer">http://pastebin.com/SVnciGKR</a> <br/>
  My properties file is available here: <a href="http://pastebin.com/9LCtNQUq" rel="nofollow noreferrer">http://pastebin.com/9LCtNQUq</a></p>
  <hr />
  <h2>UPDATE</h2>
  <p>By modifying my targets to look like below, I was able to get things working. Unfortunately, I have to manually embed ant-junit.jar and junit.jar into my repository, but it works, so I guess this solves it. If anyone can help me get rid of the need to embed ant-junit.jar and junit.jar, I'd really appreciate it:</p>
  <pre><code>&lt;path id=&quot;webapp.tests.path&quot; &gt;
      &lt;pathelement location=&quot;${red5.home}/red5.jar&quot;/&gt;
      &lt;pathelement location=&quot;${red5.home}/boot.jar&quot;/&gt;
      &lt;pathelement location=&quot;${bin.dir}/${ant.project.name}.jar&quot;/&gt;
      &lt;pathelement path=&quot;${red5.lib.dir}&quot;/&gt;
      &lt;pathelement path=&quot;${main.java.lib.dir}&quot;/&gt;
      &lt;pathelement path=&quot;${test.java.lib.dir}&quot;/&gt;
  &lt;/path&gt;
  
  &lt;target name=&quot;compile.webapp.tests&quot; depends=&quot;compile.webapp&quot;&gt;
      &lt;javac srcdir=&quot;${test.java.src.dir}&quot;
              destdir=&quot;${test.java.bin.dir}&quot;&gt;
          &lt;classpath refid=&quot;webapp.tests.path&quot;/&gt;
      &lt;/javac&gt;
  &lt;/target&gt;
  
  &lt;target name=&quot;run.webapp.tests&quot; depends=&quot;compile.webapp.tests&quot;&gt;     
      &lt;junit printsummary=&quot;true&quot;&gt;
          &lt;classpath&gt;
              &lt;path refid=&quot;webapp.tests.path&quot;/&gt;
              &lt;pathelement location=&quot;${test.lib.dir}/ant/ant-junit.jar&quot;/&gt;
              &lt;pathelement location=&quot;${test.lib.dir}/ant/junit-4.8.2.jar&quot;/&gt;
              &lt;pathelement path=&quot;${test.java.bin.dir}&quot;/&gt;
          &lt;/classpath&gt;
          
          &lt;formatter type=&quot;xml&quot;/&gt;
          
          &lt;batchtest todir=&quot;${test.java.output.dir}&quot;&gt;
              &lt;fileset dir=&quot;${test.java.bin.dir}&quot; includes=&quot;**/*TestCase*&quot;/&gt;
          &lt;/batchtest&gt;
      &lt;/junit&gt;
      
      &lt;junitreport todir=&quot;${test.java.report.dir}&quot;&gt;
          &lt;fileset dir=&quot;${test.java.output.dir}&quot; includes=&quot;**/*&quot;/&gt;
          &lt;report todir=&quot;${test.java.report.dir}&quot;/&gt;
      &lt;/junitreport&gt;
      
      &lt;delete file=&quot;${test.java.report.dir}/TESTS-TestSuites.xml&quot;/&gt;
  &lt;/target&gt;
  </code></pre>
  <p>If I don't include the jars in the classpath, I get errors telling me that junit.jar must be on the classpath in order to run the <code>&lt;junit&gt;</code> task. Weird, huh?</p>