2

I'm trying to run a JUnit test case from command line using this command:

F:\>java org.junit.runner.JUnitCore org.junit4.9b2.junit.SimpleTest

but I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore

Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
        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)
Could not find the main class: org.junit.runner.JUnitCore.  Program will exit.

What is the problem?

reevesy
  • 3,452
  • 1
  • 26
  • 23
Adham
  • 63,550
  • 98
  • 229
  • 344

1 Answers1

8

Obviously you need junit on the classpath :-)

java -cp path/to/junit.jar:path/to/local/classes org.junit.runner.JUnitCore \
         org.junit4.9b2.junit.SimpleTest

(replace the : with ; on windows platforms)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 1
    I already did .. this is the value of CLASSPATH .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;F:\junit\junit4.9b2;F:\junit\junit4.9b2\junit-4.9b2.jar;F:\junit\junit4.9b2\org\junit\samples – Adham Mar 02 '11 at 16:53
  • How can i specify execute one method of the testcase class?thanks – Mr Lou Jun 30 '12 at 03:44
  • @janwen it doesn't seem like you can do that from the command line. But you should ask a separate question if you want to be sure. I know you can do it from Maven or Eclipse, but not from the CLI – Sean Patrick Floyd Jun 30 '12 at 08:03
  • thanks @SeanPatrickFloyd ,I am going to write shell script to do this. – Mr Lou Jul 01 '12 at 10:05
  • @janwen for a single test case see http://stackoverflow.com/questions/9288107/run-single-test-from-a-junit-class-using-command-line – reevesy Jul 18 '12 at 09:41