Issue
I have a folder(which is where I am running all commands) called learning
. Inside this folder, I have two files, one is called Driver.java
which is a simple main class with a simple hello world method. The other file is DriverTest.java
which has this code as shown below.
import static org.junit.Assert.*;
import org.junit.Test;
public class DriverTest {
@Test
public void test() {
fail("Not yet implemented");
}
}
Apart from this I have JUnit 4.13-beta jar inside the same learning
folder.
Now I open command line in windows and go to learning
folder location and run this command.
javac -cp junit-4.13-beta-1.jar;hamcrest-core-2.1-rc4.jar;. *.java
It didn't give me any errors, hence it has compiled both Driver
and DriverTest
java files.
Now I am trying to run the JUnit test using this command.
java -cp junit-4.13-beta-1.jar;hamcrest-core-2.1-rc4.jar;. org.junit.runner.JUnitCore DriverTest
But I am getting this error:
JUnit version 4.13-beta-1
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(JUnitCommandLineParseResult.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
Why is my HamCrest
jar not found? Its right there in the same folder right?
Solution
You only added the current directory to the classpath for compilation. This needs to also be done for test invocation, ie. ;.
in windows :.
on linux
java -cp junit-4.13-beta-1.jar;. org.junit.runner.JUnitCore DriverTest
Answered By - Gregory Ostermayr
Answer Checked By - Gilberto Lyons (JavaFixing Admin)