java.lang.NoClassDefFoundError in junit
Solution 1
- Right click your project in Package Explorer > click Properties
- go to Java Build Path > Libraries tab
- click on 'Add Library' button
- select JUnit
- click Next.
- select in dropdown button JUnit4 or other new versions.
- click finish.
- Then Ok.
Solution 2
- Eclipse -> Top menu -> Run -> Run Configurations
- Delete all the occurrences of your test. Your test may appear as YourTest.Method_1(). Delete that as well.
- Re-run. Let Eclipse build a fresh configuration.
Addendum: Locally I have created a "User Library" and added to my projects which has
hamcrest-core-1.3.jar
junit-4.12.jar
Solution 3
The same problem can occur if you have downloaded JUnit jar from the JUnit website, but forgotten to download the Hamcrest jar - both are required (the instructions say to download both, but I skipped ahead! Oops)
Solution 4
When using in Maven, update artifact junit:junit from e.g. 4.8.2 to 4.11.
Solution 5
On Eclipse I was able to solve the above issue by following the below steps :
Right-click on the test file which you want to run, Select Run As -> Run Configurations -> Select Classpath tab -> Select to the bootstrap Entries -> Select Advanced -> Select Add library -> Select JUnit -> Next ->Select JUnit4 from the drop-down -> Finish
Then Select Apply -> Run
Admin
Updated on October 14, 2021Comments
-
Admin over 2 years
I am getting this error in java in my junit test code. I looked up on the net and it says that I need to add the junit.jar in the classpath.
In Eclipse I have added it in the classpath of Project Properties windows but I still get initialisation error. What should I do..?
This is the complete trace of the error:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source) 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 org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 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) ... 25 more
-
Admin over 13 yearsDoing this adds JUNIT in library but now it says that junit.org cannot be resolved
-
Berin Loritsch over 13 yearsJust double-check that you have the Hamcrest library also on the classpath. The current version of JUnit 4 should be fully inclusive, but sometimes folks split up packages.
-
Vipin Verma almost 10 yearshow do i proceed if i am using the linux command line? where do i add this jar?
-
Alex almost 9 yearsThis fixed a
NoClassDefFoundError: org/junit
...caused by ClassNotFoundException: org.junit.runners.model.InitializationError
for me. I was using Junit 4.8. Moving to 4.11 worked. -
Vineeth Bhaskaran about 8 yearsThe above steps is working fine and it is taking jar from eclipse plugin folder. But if we add junit jar file in configure build path as external jar it should work right? it was not working for me