title: Ant JUnit ClassNotFoundException
body: <p>I realize there are a lot of similar questions as <a href="https://stackoverflow.com/questions/10658636/new-to-ant-classnotfoundexception-with-junit">this one</a>, however after reading over most of them I seem to be having the same problem, but for different reasons.</p>
  
  <p>I'm running a <code>&lt;junit&gt;</code> task inside my Ant build and am getting a <code>ClassNotFoundException</code>. In most of the similarly-related questions (such as the one above), this turned out to be various degrees of the author simply not configuring the JUnit classpath correctly. Although it may very well be the same for me, none of the suggestions to any of these other questions have worked for me.</p>
  
  <p>My project directory structure:</p>
  
  <pre><code>MyProject/
      src/main/java/
          FakeObject.java
          ...the rest of my Java main source
      src/test/java
          FakeObjectUnitTest.java
          ...the rest of my Java test source
      bin/main
          FakeObject.class
          ...the rest of main Java binaries
      bin/test
          FakeObjectUnitTest.class
          ...the rest of main Java binaries
      lib/main
          ...my main dependencies
      lib/test
          junit-4.10.jar
          hamcrest-1.1.jar
          ...the rest of my test dependencies
      gen/reports
          ...where JUnit should be placing all test reports
      build/
          build.xml
          build.properties
  </code></pre>
  
  <p>In <code>src/test/java/FakeObjectUnitTest.java</code>:</p>
  
  <pre><code>// Package and imports omitted
  
  public class FakeObjectUnitTest {
      @Test
      public void doSomething() {
          int x = 5;
  
          Assert.assertEquals(x, 5);
      }
  }
  </code></pre>
  
  <p>My <code>build.xml</code>:</p>
  
  <pre><code>&lt;project name="" basedir=".."&gt;
      &lt;!-- Main classpath. --&gt;
      &lt;path id="main.class.path"&gt;
          &lt;fileset dir="bin/main"/&gt;
      &lt;/path&gt;
  
      &lt;!-- Test classpath. --&gt;
      &lt;path id="test.class.path"&gt;
          &lt;fileset dir="bin/test"/&gt;
      &lt;/path&gt;
  
      &lt;!-- JUnit classpath. --&gt;
      &lt;path id="junit.class.path"&gt;
          &lt;fileset dir="lib/main" includes="*.jar"/&gt;
          &lt;fileset dir="lib/test" includes="*.jar"/&gt;
          &lt;path refid="main.class.path"/&gt;
          &lt;path refid="test.class.path"/&gt;
      &lt;/path&gt;
  
      &lt;!--
          All previous targets omitted for brevity (such as for compiling, etc.), but
      I assure you all that they work and compile FakeObject.java/FakeObjectUnitTest.java into
      FakeObject.class/FakeObjectUnitTest.class respectively, and place them in the correct
  bin directories.
      --&gt;
  
      &lt;target name="run-junit" depends="compile"&gt;
  
          &lt;property name="junit.path" refid="junit.class.path"/&gt;
          &lt;echo message="JUnit ClassPath is: ${junit.path}"/&gt;
  
          &lt;junit printsummary="yes" haltonerror="yes" haltonfailure="yes"&gt;
              &lt;classpath refid="junit.class.path"/&gt;
  
              &lt;formatter type="xml"/&gt;
  
              &lt;batchtest todir="gen/reports"&gt;
                  &lt;fileset dir="src/test/java"&gt;
                      &lt;include name="**/*UnitTest*.java"/&gt;
                  &lt;/fileset&gt;
              &lt;/batchtest&gt;
          &lt;/junit&gt;
      &lt;/target&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>When I run this target from the command-line:</p>
  
  <pre><code>run-junit:
      [echo] Conducting unit tests with JUnit.
      [echo] JUnit ClassPath is: /&lt;path-to-my-project&gt;/MyProject/lib/test/hamcrest-1.1.jar:/&lt;path-to-my-project&gt;/MyProject/lib/test/junit-4.10.jar:/&lt;path-to-my-project&gt;/MyProject/bin/main/com/myproject/client/FakeObject.class:/&lt;path-to-my-project&gt;/MyProject/bin/test/com/myproject/client/FakeObjectUnitTest.class
      [junit] Running com.myproject.client.FakeObjectUnitTest
      [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
  </code></pre>
  
  <p>I do this to <em>prove</em> that it sees <code>FakeObjectUnitTest.class</code> on the JUnit classpath. However, it fails with an error. When I go into the XML TEST report for this test, I see the following:</p>
  
  <pre><code>&lt;error message="com.myproject.client.FakeObjectUnitTest" type="java.lang.ClassNotFoundException"&gt;java.lang.ClassNotFoundException: com.myproject.client.FakeObjectUnitTest
      at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:247)
  &lt;/error&gt;
  </code></pre>
  
  <p>When I run Ant in verbose mode (<code>-v</code>) the only <em>hint</em> I see are a bunch of warnings along the lines of:</p>
  
  <blockquote>
    <p>Ignoring Exception java.util.zip.ZipException: error in opening zip file reading resource x</p>
  </blockquote>
  
  <p>...where <code>x</code> is the name of a class file.</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/6015092/zipexception-when-running-junit-tests">this</a> question on SO, and the problem turned out to be that the author had added class files to the classpath, not JARs (which makes JUnit unhappy). So I tried removing <code>main.class.path</code> and <code>test.class.path</code> from <code>junit.class/path</code> and then I get an exception that none of the class files can be found.</p>
  
  <p>Any ideas or thoughts as to what is going on? Thanks in advance, and sorry for the <code>-v</code> question here, but I figured the more details, the better.</p>