90

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

17 Answers17

115

The problem is an incomplete runtime classpath. Your code may compile fine in the IDE but it needs additional classes (jars) at runtime.

ClassNotFoundException: org.hamcrest.SelfDescribing is indicating which class is missing. org.hamcrest is required by JUnit any it may be missing in your project setup.

Try the following steps to add all of the JUnit4 libraries (including required runtime jars) to the project's classpath:

  1. Right click your project in Package Explorer > click Properties
  2. go to Java Build Path > Libraries tab
  3. click on 'Add Library' button
  4. select JUnit
  5. click Next.
  6. select in dropdown button JUnit4 or other new versions.
  7. click finish.
  8. Then Ok.

Note, the steps are for a pure Eclipse project. If you use Maven, Gradle or another tool for dependency management you need to add the dependency there and refresh the project setup in Eclipse. This usually happens automatically with the proper integration.

Gunnar
  • 2,264
  • 17
  • 31
Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
  • Doing this adds JUNIT in library but now it says that junit.org cannot be resolved –  Nov 19 '10 at 18:18
  • 5
    Just 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. – Berin Loritsch Nov 19 '10 at 18:18
  • how do i proceed if i am using the linux command line? where do i add this jar? – Vipin Verma Jun 20 '14 at 01:49
  • The 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 – Vineeth Bhaskaran Mar 21 '16 at 09:06
20
  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

Community
  • 1
  • 1
Hado99
  • 555
  • 6
  • 15
12

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)

Alex
  • 18,332
  • 10
  • 49
  • 53
11

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

Tires
  • 1,529
  • 16
  • 27
  • 1
    This 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. – Alex May 29 '15 at 08:49
8

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

sana
  • 763
  • 9
  • 19
3

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

Daniel Cheung
  • 4,779
  • 1
  • 30
  • 63
2

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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
2

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

marcor92
  • 417
  • 5
  • 13
1

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.

Defrag
  • 146
  • 1
  • 5
1
  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
AnandSonake
  • 567
  • 1
  • 8
  • 19
1

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.
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
1

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.

badbishop
  • 1,281
  • 2
  • 18
  • 38
1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Woden
  • 1,054
  • 2
  • 13
  • 26
1

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.

Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
Amit
  • 11
  • 1
0

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.

Ujjwal
  • 603
  • 12
  • 23
0

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

-1

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.

Bin
  • 9