3

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?

Roland Weisleder
  • 9,668
  • 7
  • 37
  • 59
User 19826
  • 509
  • 2
  • 5
  • 13

1 Answers1

4

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
Dmytro Maslenko
  • 2,247
  • 9
  • 16
  • Thanks. I copied the jar files from C:\JUnit to my practice folder C:\Prac, and ran the command you suggested. It still gives me errors. I was assuming since I have set the classpath then I don't have to include the jar folders under C:\Prac. But anyways it still gives errors. – User 19826 Feb 01 '19 at 03:47
  • Today I had more time and played with it locally. I was able to reproduce it and found the root cause of issue - wrong hamcrest jar. I have updated my post above. – Dmytro Maslenko Feb 01 '19 at 19:15