Kevin
Kevin

Reputation: 636

JUnit sample program compiling but giving runtime error

I am starting off with JUnit in Ubuntu and tried to execute the sample program given here. I have also followed instructions as given in the same link. The code compiles succesfully but gives runtime error as follows:

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
    at TestRunner.main(TestRunner.java:7)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

So far this is what I have got. This error occurs if the $CLASSPATH variable isn't set to the location of the classes required by it.

> echo $CLASSPATH
/home/webyog/workspace/JUNIT/junit-4.11.jar:.

My java files and class files are in /home/webyog/JUNIT_WORKSPACE/ and all the libraries and the JUnit jar file is in /home/webyog/workspace/JUNIT/. I executed these in order:

javac TestJunit.java TestRunner.java -classpath /home/webyog/workspace/JUNIT/junit-4.11

(This generated 2 files TestRunner.class and TestJunit.class)

java TestRunner -classpath $CLASSPATH

(This gives the error)

What I am I missing? Please help.

Upvotes: 0

Views: 220

Answers (1)

user180100
user180100

Reputation:

In your question, you have /home/webyog/workspace/JUNIT/junit-4.11.jar:. for $CLASSPATH but state:

the JUnit jar file is in /home/webyog/JUNIT/

(note the workspace missing)

That could explain the error you are seeing.

Try with:

cd /home/webyog/JUNIT_WORKSPACE/
java -cp '/home/webyog/JUNIT/junit-4.11.jar:.' TestRunner 

Also you might want to check out some build tool to make you life easier (maven, ant, gradle, ...)

Upvotes: 1

Related Questions