2

I'm new to running java from the terminal and am currently trying to run a JUnit test class in terminal. The basic structure of my java classes is

JavaProject
    Queue.java
    QueueTests.java
    junit.jar
    RunTests

RunTests is a script with the following code.

 #!/bin/sh
 javac Queue.java
 CLASSPATH=".:junit.jar:"; export CLASSPATH
 javac QueueTests.java
 java org.junit.runner.JUnitCore QueueTests

However, when I run this, I always get a "Exception in thread "main" java.lang.NoClassDefFoundError" error from the final line. If i comment out the final line, there are no errors. As such, it seems like it is unable to find QueueTests.class. However, looking at the folder, it is clear after I run the script that both Queue.class and QueueTests.class are there. I am not sure what I am doing wrong. Any help would really be appreciated.

user3684980
  • 269
  • 1
  • 5
  • 13

2 Answers2

0

In the JavaProject folder, execute the following command:

java -cp .:./junit.jar org.junit.runner.JUnitCore QueueTests

To adapt your script, that would be

#!/bin/sh
javac Queue.java QueueTests.java
CLASSPATH=".:junit.jar:"
java -cp $CLASSPATH org.junit.runner.JUnitCore QueueTests   

See also the following question: How to run JUnit test cases from the command line

Community
  • 1
  • 1
user3001
  • 3,437
  • 5
  • 28
  • 54
-1

If you are trying to learn and experiment new things like this in java without having to load up a full/heavy IDE and meanwhile without having to run lengthy terminal commands I would recommend you to use a lightweight java IDE like Dr Java.

PS: I am assuming that you are running from terminal just to avoid the hassle of IDE to execute and run smaller tasks like this faster.

enter image description here

Sudip Bhandari
  • 2,165
  • 1
  • 27
  • 26