Ant + JUnit: NoClassDefFoundError

41,440

Solution 1

I downloaded JUnit 4.7 and put junit-4.7.jar in my build path (instead of the older version). That solved it. I didn't touch ant.

Solution 2

add also hamcrest.jar to your testing classpath

Solution 3

I found the reason to an identical problem, but only when running the test with Ant on the commandline.

You might want to run Ant in debug mode (ant -d) and watch out for this statement:

[junit] Implicitly adding /usr/share/ant/lib/junit4.jar:...

Im my case, this JAR was added to the end of my classpath when running the test and overrode my JUnit reference in my Ant script.

It turned out that my Ant installation was bundled with a set of older JUnit libs. I had to remove the junit4.jar above in order to make my Ant classpath ref to take effect.

Solution 4

Faced exactly the same problem in eclipse but resolved it following the given steps

  1. Go to Preferences | Java | JUnit
  2. Click "Add Package" and add "org.hamcrest.*"

Voila... the problem is gone..

Solution 5

I had same problem with jUnit 4.11, Ant and Ivy. This code works for me but you have to make sure that you have hamcrest-core-1.3.jar (for jUnit 4.11) in your lib folder.

<property name="lib.dir">lib</property>
<property name="test.dir">src/test/java</property>
<property name="build.test">build/test/java</property>

<path id="test.path.id">
    <path location="${build.test}" />
    <path refid="lib.path.id" />
</path>

<path id="lib.path.id">
    <fileset dir="${lib.dir}" />
</path>

<!-- ================================= 
              Runs Tests         
================================= -->
<target name="junit">
    <junit>
        <classpath refid="test.path.id" />
        <batchtest>
            <fileset dir="${test.dir}" />
        </batchtest>
        <formatter type="plain" usefile="false" />
    </junit>
</target>
Share:
41,440

Related videos on Youtube

SteveT
Author by

SteveT

Updated on July 09, 2022

Comments

  • SteveT
    SteveT almost 2 years

    Ok, I'm frustrated! I've hunted around for a good number of hours and am still stumped.

    Environment: WinXP, Eclipse Galileo 3.5 (straight install - no extra plugins).

    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:

    @Test
    public void testX() {
        assertEquals("1", new Integer(1).toString());
    }
    

    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).

    So, I create a build.xml:

    <project name="Test" default="basic">
        <property name="default.target.dir" value="${basedir}/target" />
        <property name="test.report.dir" value="${default.target.dir}/test-reports" />
    
        <target name="basic">
            <mkdir dir="${test.report.dir}" />
            <junit fork="true" printSummary="true" showOutput="true">
                <formatter type="plain" />
                <classpath>
                    <pathelement path="${basedir}/bin "/>
                </classpath>
                <batchtest fork="true" todir="${test.report.dir}" >
                    <fileset dir="${basedir}/bin">
                        <include name="**/*Test.*" />
                    </fileset>
                </batchtest>
            </junit>
        </target>
    </project>
    

    ${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.

    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.

    So, what happens when I run this insanely complex target? My report file contains:

    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)
    

    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.

    Stumped (and frustrated)!!

    • Raul Luna
      Raul Luna about 9 years
      In my case, this problem came up when moved a project from java 1.6 to java 1.5. I don't know if this matters.
  • SteveT
    SteveT over 14 years
    Thanks for all the replies. We're on to something, but not solved yet. I downloaded junit-4.6.jar from junit.org. It contains the org.hamcrest classes. I add that jar to the classpath and still get the same error. I do not have a stray Ant or JUnit install floating around someplace.
  • SteveT
    SteveT over 14 years
    This is crazy. More info... The Java project has the Eclipse-standard JUnit4 library. That library includes two jars - plugins/org.junit4_4.5.0.v20090423/junit.jar plugins/org.hamcrest.core_1.1.0.v20090501071000.jar In my Ant Global Entries I have added those two jars. When I run the task I get the exact same error. A NCDFE on org.hamcrest.SelfDescribing. Completely baffled...
  • Kushal Paudyal
    Kushal Paudyal about 12 years
    This solved my problem. I actually had to do fork="off". I spent my three days trying to figure out how to solve, and finally after trying your suggestion got that thing working.Thanks a bunch.
  • Conrad.Dean
    Conrad.Dean about 11 years
    cd lib and wget http://repo2.maven.org/maven2/org/hamcrest/hamcrest-all/1.1/‌​hamcrest-all-1.1.jar for anyone else that got here from Google
  • Anu Shibin Joseph Raj
    Anu Shibin Joseph Raj about 3 years
    Same case with me. I removed ant-junit4.jar from ANT lib and it worked.