User 19826
User 19826

Reputation: 599

JUnit - java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

I am using Win 7 and downloaded Junit 4.13 and hamcrest 2.1 from https://github.com/junit-team/junit4/wiki/Download-and-Install

and put them in a folder named JUnit under C.

I have added the variable: JUNIT_HOME with value C:\JUnit.

I have also added the following to my classpath:

%classpath%;.;C:\Program Files\Java\jre1.8.0_171\bin;%CLASSPATH%; 
%JUNIT_HOME%\junit-4.13-beta-1;.; %JUNIT_HOME%\hamcrest-core-2.1.jar;

Following https://github.com/junit-team/junit4/wiki/Getting-started, I added the java files Calculator.java and CalculatorTest.java to my practice folder.

The command

javac -cp .;junit-4.13-beta-1.jar;hamcrest-core-2.1.jar CalculatorTest.java

is executed successfully. However, the command

java -cp .;junit-4.13-beta-1.jar;hamcrest-core-2.1.jar org.junit.runner.JUnitCore CalculatorTest

Fails, giving an error message like

Exception in thread "main" 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$100(Unknown Source)
    at java.net.URLClassLoader$1.run(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.runner.Computer.getSuite(Computer.java:28)
    at org.junit.runner.Request.classes(Request.java:77)
    at org.junit.runner.JUnitCommandLineParseResult.createRequest(JUnitComma
ndLineParseResult.java:116)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
    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)
    ... 17 more

Please note I have checked posts like java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing, but they have not been helpful. Could you please help?

Upvotes: 3

Views: 5445

Answers (1)

Dmytro Maslenko
Dmytro Maslenko

Reputation: 2297

The correct hamcrest jar is hamcrest-2.1.jar, not hamcrest-core-2.1.jar. You can download it from here.


According to mentioned guide it is assumed you have downloaded jars in the same package as your project:

Create a new folder junit-example and download the current junit-4.XX.jar from JUnit's release page and Hamcrest to this folder.

If you decided to put jars separately probably you need to correct the command line correspondingly to take these jars files from %JUNIT_HOME%:

java -cp .;%JUNIT_HOME%\junit-4.13-beta-1.jar;%JUNIT_HOME%\hamcrest-core-2.1.jar org.junit.runner.JUnitCore CalculatorTest

Upvotes: 4

Related Questions