Ant + JUnit: NoClassDefFoundError

Ok, I'm upset! I hunted around for hours, and I'm still at a standstill.

Environment: WinXP, Eclipse Galileo 3.5 (direct installation - no additional plugins).

So, I have a simple JUnit test. It works great from within the internal configuration of Eclipse JUnit. This class has nothing to do with anything. To minimize this problem, it simply contains:

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

No sweat yet. Now I want to take the super-advanced step of launching this test case from Ant (the final goal is integration with Hudson).

So, I create a build.xml file:

 <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 name of the Java project in the workspace that contains the source code, classes, and assembly file. All .java and build.xml are in $ {basedir} / src. The .class files are in $ {basedir} / bin.

I 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 launch this insanely challenging goal? 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? Do something with ridicule? Good. But why addiction? I do nothing with this. This is literally a Java project without dependencies other than JUnit.

Inserted (and upset) !!

+26
java classpath junit ant hamcrest
Jul 23 '09 at 11:56
source share
15 answers

I downloaded JUnit 4.7 and put junit-4.7.jar in my build path (instead of the old version). It decided. I did not touch ant.

+14
Sep 20 '09 at 19:22
source share

add also hamcrest.jar to your test class path

+10
Jul 23 '09 at 11:59
source share

I found the cause of an identical problem, but only when running the test with Ant on the command line.

You might want to run Ant in debug mode ( ant -d ) and keep track of this statement:

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

In my case, this JAR was added at the end of my class path when running the test and redefined my JUnit link in my Ant script.

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

+6
Jun 14 '10 at 11:24
source share

Faced with the same problem in eclipse but resolving it, follow these steps

  • Go to settings | Java | Junit
  • Click "Add Package" and add "org.hamcrest. *"

Howled ... the problem is gone.

+3
Feb 03 '12 at 21:00
source share

I had the same problem and tried to change the classpath. The simplest solution for me was to use the fork = "no" parameter at the junit target point. It seems that Eclipse can "automatically" do it better than me ...

+2
Aug 23 '09 at 15:24
source share

I had the same issue with jUnit 4.11, Ant and Ivy. This code works for me, but you have to make sure 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> 
+2
Jun 21. '13 at 11:45
source share

I searched the Eclipse installation directory from the top level for "hamcrest.jar" (* * wildcard around hamcrest that does not appear in this web post). Then I added the found jar file to the class path in Ant build.xml. Fixed.

+1
Nov 12 '09 at 17:03
source share

I had this problem with trying to run junit tests from Run -> Run As -> JUnit Test (I checked and I have the latest junit plugin installed (junit4_4.5.0)).

I created a new directory (externalJars) in the main eclipse installation directory, downloaded the latest junit jar (4.8.1) there.

Next, from Eclipse, with the project selected, I changed the jar files in the build path (Project -> Properties -> Java Build Path). When I first tried junit, I added the junit jar file from the plugin / org.junit4_4.5.0.v20090824 folder. I deleted this jar file, and I added the external Jars / junit-4.8.1.jar (the one I just downloaded) to the build path by clicking the "Add external jars" button.

hamcrest.org is a matches library. You can find information at:

http://code.google.com/p/hamcrest/

Provides a library of matching objects (also called constraints or predicates) that allow you to declaratively define matching rules that will be used in other frameworks. Typical scenarios include test frameworks, mocking libraries, and UI validation rules.

Hamcrest has been ported to Java, C ++, Objective-C, Python, and PhP.

Note: Hamcrest is not a test library: it just happens that matches are very useful for testing.

+1
Feb 04 '10 at 15:17
source share

I added hamcrest and junit jar files to the java / lib / ext directory and it worked fine for me.

+1
Aug 12 '13 at 13:05
source share

JUnit 4.5 includes Hamcrest as part of its matching system. You want you to have this JAR file on your class path.

0
Jul 23 '09 at 11:59
source share

The class that you are missing is included in junit from version 4.4 onwards ( link ).

Check if an older version of jUnit is in your classpath. Try running the ant script in the shell to find out if you have the same error. If you do this, then probably ant uses jUnit version older than 4.4

Update:

Check out the ANT_HOME folder. Right-click the build.xml file, select the second "Ant build" in the "Run as" dialog box. A dialog box opens to edit the ant startup configuration (there might be an easier way to open this). Click the Classpath tab. Add a new junit to the classpath (lib ant folder).

0
Jul 23 '09 at 12:03
source share

I don’t think it is necessary to add a JUnit jar to your Ant path. Instead, make sure you have the JUnit library in your Java build path (Windows / Preferences / Java / Build Path / Classpath variables).

0
Jul 23 '09 at 12:38
source share

OK I finally circumvented this issue. I will not say "permitted", as I am still rather confused about what is happening. Anyway...

I downloaded the latest Ant (1.7.1) and JUnit (4.6) and blew them into an external directory - NOT in the workspace or in "Eclipse".

Then I went to Windows / Preferences / Ant / Runtime / Classpath / Ant Home Entries and will kill all existing (default) values. I replaced them with the contents of the recently downloaded Ant lib directory.

Then I added the uploaded junit.jar to Global Entries. It contains org.hamcrest classes, like junit.jar included in Eclipse, so I don't know why this is necessary, but it is.

What is it. My junit task in the build file works fine.

0
Jul 23 '09 at 15:20
source share

In eclipse, this happens when you add junit jar with Properties->Libaries->Add External JARs...

Instead of using Properties->Libaries->Add Library... , which allows you to select Junit3 or Junit4 and automatically adds the hamcrest library to the path.

0
Jul 05 '13 at 17:32
source share

I had a similar problem that I overcame by placing a copy of "hamcrest-core-1.3.jar" in the \ lib folder.

0
Dec 23 '13 at 4:56
source share



All Articles