title: Ant + JUnit: NoClassDefFoundError
body: <p>Ok, I'm frustrated!  I've hunted around for a good number of hours and am still stumped.</p>
  
  <p>Environment: WinXP, Eclipse Galileo 3.5 (straight install - no extra plugins).</p>
  
  <p>So, I have a simple JUnit test.  It runs fine from it's internal Eclipse JUnit run configuration.  This class has no dependencies on anything.  To narrow this problem down as much as possible it simply contains:</p>
  
  <pre><code>@Test
  public void testX() {
      assertEquals("1", new Integer(1).toString());
  }
  </code></pre>
  
  <p>No sweat so far.  Now I want to take the super advanced step of running this test case from within Ant (the final goal is to integrate with Hudson).</p>
  
  <p>So, I create a build.xml:</p>
  
  <pre><code>&lt;project name="Test" default="basic"&gt;
      &lt;property name="default.target.dir" value="${basedir}/target" /&gt;
      &lt;property name="test.report.dir" value="${default.target.dir}/test-reports" /&gt;
  
      &lt;target name="basic"&gt;
          &lt;mkdir dir="${test.report.dir}" /&gt;
          &lt;junit fork="true" printSummary="true" showOutput="true"&gt;
              &lt;formatter type="plain" /&gt;
              &lt;classpath&gt;
                  &lt;pathelement path="${basedir}/bin "/&gt;
              &lt;/classpath&gt;
              &lt;batchtest fork="true" todir="${test.report.dir}" &gt;
                  &lt;fileset dir="${basedir}/bin"&gt;
                      &lt;include name="**/*Test.*" /&gt;
                  &lt;/fileset&gt;
              &lt;/batchtest&gt;
          &lt;/junit&gt;
      &lt;/target&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>${basedir} is the Java project name in the workspace that contains the source, classes and build file.  All .java's and the build.xml are in ${basedir}/src.  The .class files are in ${basedir}/bin.</p>
  
  <p>I have added eclipse-install-dir/plugins/org.junit4_4.5.0.v20090423/junit.jar to the Ant Runtime Classpath via Windows / Preferences / Ant / Runtime / Contributed Entries.  ant-junit.jar is in Ant Home Entries.</p>
  
  <p>So, what happens when I run this insanely complex target?  My report file contains:</p>
  
  <pre><code>Testsuite: com.xyz.test.RussianTest
  Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
  
  Testcase: initializationError took 0 sec
  Caused an ERROR
  org/hamcrest/SelfDescribing
  java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(Unknown Source)
      at java.security.SecureClassLoader.defineClass(Unknown Source)
      at java.net.URLClassLoader.defineClass(Unknown Source)
      at java.net.URLClassLoader.access$000(Unknown Source)
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClassInternal(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClassInternal(Unknown Source)
  </code></pre>
  
  <p>What is this org.hamcrest.SelfDescribing class?  Something to do with mocks?  OK, fine.  But why the dependency?  I'm not doing anything at all with it.  This is literally a Java project with no dependencies other than JUnit.</p>
  
  <p>Stumped (and frustrated)!!</p>