[java] java.lang.NoClassDefFoundError in junit

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

This question is related to java eclipse junit noclassdeffounderror

The answer is


When using in Maven, update artifact junit:junit from e.g. 4.8.2 to 4.11.


  1. Eclipse -> Top menu -> Run -> Run Configurations
  2. Delete all the occurrences of your test. Your test may appear as YourTest.Method_1(). Delete that as well.
  3. 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


I had the same issue, the problem was in the @ContextConfiguration in me test classes, i was loading the servlet context too i just change:

@ContextConfiguration(locations = { "classpath*:**\*-context.xml", "classpath*:**\*-config.xml" })

to:

@ContextConfiguration(locations = { "classpath:**\*-context.xml", "classpath:**\*-config.xml" })

and that´s it. this way im only loading all the files with the pattern *-context.xml in me test path.


The org/hamcrest/SelfDescribing class is not on the run-time classpath.


  1. Make sure Environment variable JUNIT_HOME is set to c:\JUNIT.
  2. then In run configuration > select classpath > add external jars junit-4.11.jar

Try below steps:

  1. Go to your project's run configuration. In Classpath tab add JUnit library.
  2. Retry the same steps.

It worked for me.


These steps worked for me when the error showed that the Filter class was missing (as reported in this false-diplicated question: JUnit: NoClassDefFoundError: org/junit/runner/manipulation/Filter ):

  1. Make sure to have JUnit 4 referenced only once in your project (I also removed the Maven nature, but I am not sure if this step has any influence in solving the problem).
  2. Right click the file containing unit tests, select Properties, and under the Run/Debug settings, remove any entries from the Launch Configurations for that file. Hit Apply and close.
  3. Right click the project containing unit tests, select Properties, and under the Run/Debug settings, remove any entries involving JUnit from the Launch Configurations. Hit Apply and close.
  4. Clean the project, and run the test.

Thanks to these answers for giving me the hint for this solution: https://stackoverflow.com/a/34067333/5538923 and https://stackoverflow.com/a/39987979/5538923).


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


If you have more than one version of java, it may interfere with your program.

I suggest you download JCreator.

When you do, click configure, options, and JDK Profiles. Delete the old versions of Java from the list. Then click the play button. Your program should appear.

If it doesn't, press ctrl+alt+O and then press the play button again.


even Junit4.11.jar doesnot have the hamcrest-core.jar. I added explicitly in the classpath and the issue was resolved.


The reason for this is "hamcrest-core" jar is not in classpath as it doesn't comes directly with junit jar. So there are two ways to resolve this:

  1. select project -> buildpath -> add libraries and select junit (It contains both junit & hamcrest-core)
  2. download hamcrest-core from maven repo and add this to your classpath.

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)


I was following this video: https://www.youtube.com/watch?v=WHPPQGOyy_Y but failed to run the test. After that, I deleted all the downloaded files and add the Junit using the step in the picture.

enter image description here


This error also comes if 2 versions of hamcrest-library or hamcrest-core is present in the classpath.

In the pom file, you can exclude the extra version and it works.


Adding my two cents to other answers.

Check if you haven't by any chance created your test class under src/main/java instead of usual src/test/java. The former is the default in Eclipse when you create a new test class for whatever reason and can be overlooked. It can be as simple as that.


In my case, I added my libraries to Modulepath instead on Classpath.

It only works if JUnit is correctly added to Classpath.

enter image description here


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to junit

Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to resolve Unneccessary Stubbing exception JUnit 5: How to assert an exception is thrown? How do I mock a REST template exchange? Class Not Found: Empty Test Suite in IntelliJ Unable to find a @SpringBootConfiguration when doing a JpaTest Failed to load ApplicationContext (with annotation) Example of Mockito's argumentCaptor Mockito - NullpointerException when stubbing Method Spring jUnit Testing properties file

Examples related to noclassdeffounderror

java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory How to solve java.lang.NoClassDefFoundError? setting JAVA_HOME & CLASSPATH in CentOS 6 java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing Unable instantiate android.gms.maps.MapFragment Android java.lang.NoClassDefFoundError NoClassDefFoundError for code in an Java library on Android java.lang.NoClassDefFoundError: Could not initialize class XXX exception in thread 'main' java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError in junit