Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find or load main class org.junit.runner.JUnitCore

I packed test classes into JAR. I have junit-4.10.jar and aJar.jar in the same folder. When I try to execute:

java -cp .:junit-4.10.jar org.junit.runner.JUnitCore TestOne

Error: Could not find or load main class org.junit.runner.JUnitCore

How to make it work?

When I type: java aJar.jar:junit-4.10.jar org.junit.runner.JUnitCore TestOne

I am getting

Error: Could not find or load main class aJar.jar:junit-4.10.jar
like image 318
J.Olufsen Avatar asked Feb 01 '15 09:02

J.Olufsen


People also ask

Why could not find or load main class?

Reasons to Occur Error The error generates because the JVM fails to load the main class or package name. There are some other reasons that generate the same error, as follows: The class has been declared in the wrong package. Dependencies missing from the CLASSPATH.

Could not find or load main class when running a jar?

When the JVM is unable to locate the main class, it's often because it's looking for the corresponding . class files in the wrong classpath. Of course, the way to rectify this problem is to manually specify the classpath by either using packages or specifying the classpath.


1 Answers

You seem to be running under Windows, not LINUX/UNIX. The path separator on Windows is ;, not :. Additionally, you haven't put you jar file in the classpath. So what you want is:

java -cp aJar.jar;junit-4.10.jar org.junit.runner.JUnitCore TestOne

This of course assumes that both jars are in the current directory. You should also always avoid putting classes in the default package.

like image 132
JB Nizet Avatar answered Oct 02 '22 20:10

JB Nizet