Reputation: 4477
I have .java files and their .class files, along with junit test files, and junit-4.10.jar and hamcrest-core-1.3.jar in the same folder. I am trying to build it by doing the following:
javac ErrorTest*.java
I get a lot of errors saying
error: package org.junit does not exist
Then when I try to use the junit file in the /usr/share/java folder using this command:
javac -cp /usr/share/java/* ErrorTest*.java
I get the following error:
javac: invalid flag: /usr/share/java/ant-antlr-1.9.6.jar Usage: javac use -help for a list of possible options
How can I build and run these ErrorTest.java junit files?
Edit: Ok, I figured it out but I don't understand why.
The following gives me errors:
javac -classpath /path/to/graph/*:/path/to/graph/lib/* ErrorTest*.java
The following produces the desired .class files:
javac -classpath .:/path/to/graph/*:/path/to/graph/lib/* ErrorTest*.java
Could anyone explain why ".:" works? Much thanks.
Upvotes: 0
Views: 1174
Reputation: 199
Its becasue,Java CLASSPATH points to current directory denoted by "."
and it will look for classes in the current directory.
In case we have multiple directories defined in CLASSPATH variable, Java will look for a class starting from the first directory and only look the second directory in case it did not find the specified class in the first directory. Multiple directories are added in classpath with the help of ":"
you have set classpath like
javac -classpath .:/path/to/graph/*:/path/to/graph/lib/* ErrorTest*.java
so using this command you are setting multiple directories in classpath.
JVM search directory in the order they have listed in CLASSPATH variable.
In your case seems some classes are present in the current directory so if you are not providing current directory in classpath JVM searches in second path and then third but required class is not present so seems that it is not working
Upvotes: 2
Reputation: 199
For Running Junit 4.x test cases from command prompt you should use below command
java -cp C:\lib\junit.jar org.junit.runner.JUnitCore [test class name]
For running Junit 3.x test cases from command prompt you need use below command
java -cp /usr/java/lib/junit.jar junit.textui.TestRunner [test class name]
Example Program
**TestCaseA.java**
package test;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
public class TestCaseA
{
@Before
public void beforeMethod()
{
System.out.println("Before method..");
}
@Test
public void JUnit4Test()
{
System.out.println("In test method");
}
@After
public void afterMethod()
{
System.out.println("after method");
}
}
Execution from command prompt
java -cp junit-4.10.jar;test\TestCaseA.class; org.junit.runner.JUnitCore test.TestCaseA
Upvotes: 0