JUnit run tests command line - java

I've got the following structure
lib/junit-4.10.jar
tests/Tester.java
tests/Tester.class
build/jar/jar_file.jar
(Tester belongs to package tests)
I can compile tests using
javac -cp build/jar/jar_file.jar:lib/junit-4.10.jar tests/*.java
However I can't seem to run tests:
java -cp build/jar/jar_file.jar:lib/junit-4.10.jar org.junit.runner.JUnitCore tests.Tester
or
java -cp build/jar/jar_file.jar:lib/junit-4.10.jar org.junit.runner.JUnitCore Tester
And I get the following output:
JUnit version 4.10
Could not find class: tests.Tester
Time: 0.001
OK (0 tests)
How do I resolve this Could not find class problem? I think it may be classpath related.

Assuming this is Linux/Mac (not Windows) and your path separator is correct (:), since your test class files exist in package subdirectories under the current working directory (.)
You need to add "." to your class path, for example:
java -cp .:build/jar/jar_file.jar:lib/junit-4.10.jar org.junit.runner.JUnitCore tests.Tester

The classpath should be semi colon separated (on Windows - not sure what you are using.)
java -cp build/jar/jar_file.jar;lib/junit-4.10.jar org.junit.runner.JUnitCore tests.Tester
Also with this command line you will need to run it in your project root

Related

java classpath with junit and test class inside a package

I'm following this JUnit Getting Started Guide.
I am able to compile my test class ok:
java -cp .:junit-4.12.jar foo/bar/FancyPantsTest.java
But I'm having an issue with (I assume) how to properly set the classpath when calling java. This command fails:
java -cp .:junit-4.12.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore foo/bar/FancyPantsTest
Error I get:
There was 1 failure:
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [foo/bar/FancyPantsTest]
What's the correct way to run this?
Got it:
java -cp .:junit-4.12.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore foo.bar.FancyPantsTest
While slashes were used to refer to the FancyPantsTest source file when compiling, use dots when referring to the FancyPantsTest class file.

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
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.
I had recently the same problem (JUnit 4.12) and I managed to solve it (Windows) by the following command:
java -cp "<libs>;<relative path to .class project folder>" org.junit.runner.JUnitCore <package.ClassName>
Import to notice the "" in the classpath declaration and separated by ";"

groovy NoClassDefFoundError

I am very new to groovy and I am trying out this example from the Groovy in Action book. I have this fibonacci.groovy program and when trying to run the program with java command, I am getting the NoClassDefFound error.
The command I am using in the console is:
java -cp %GROOVY_HOME%/embeddable/groovy-all-2.2.0.jar;classes fibonacci
As you can see, I have mentioned the groovy-all jar in the classpath and I set the GROOVY_HOME variable. The classpath variable is not set, so I am assuming it has the default '.' value to find in the current folder itself. What am I doing wrong?
Aren't you missing the current folder in the classpath?
I'm on Linux, but if i compile a Groovy class with groovyc and then try to run it with java, i need to tell java where is my groovy-all.jar and also add the current dir to the classpath
So, this compilation works:
$ groovyc Fib.groovy
But this run doesn't runs:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:classes Fib
As it's missing the current dir in the path:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:. Fib
test for fib
Also note that if fibonacci is in a package, you need to type the full path to the class. So for this groovy source:
package up.foo
println "test for fib"
Compile:
$ groovyc Fib.groovy
We write the full package path to execute:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:. up.foo.Fib
There it is:
test for fib

Running JUnit 4 from command line on Eclipse project

I currently have ClasspathSuite set up to run all of my JUnit tests. I'm working on trying to get the ClasspathSuite class to run from the command line. When I am in the bin directory and run this command:
java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore MySuite.class
I get:
JUnit version 4.10
Could not find class: MySuite.class
Time: 0.002
OK (0 tests)
I also tried running the same command with the absolute path to the file, resulting in the same error message. What am I doing wrong?
Just take off the .class part. Java knows to look in that file when you specify the class name.
java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore MySuite

How do I run Groovy scripts as Java, from the command line?

I'm trying to use groovyc, but something is not right:
>echo println("Hello world") > test.groovy
>groovy test.groovy
Hello world
>groovyc test.groovy
>java -cp C:\utils\groovy-1.8.1\embeddable\groovy-all-1.8.1.jar test
Error: Could not find or load main class test
>dir test.class
...
11/10/2011 02:54 PM 7,104 test.class
What am I missing?
When you specify the classpath with -cp switch, its default value (current directory) is overwritten and so JVM can't find your class.
Add current directory to classpath, and everything works:
>java -cp C:\utils\groovy-1.8.1\embeddable\groovy-all-1.8.1.jar;. test
Hello, world
Make sure that if you are using a unix based system (Linux or Mac), then you need colon instead of semicolon for classpath entry separator:
>java -cp /path/to/groovy/embeddable/groovy-all-1.8.1.jar:. test
Hello, world
I am not sure these snippets will work, since class with main method is missed.
Proper command line is:
java -cp /path/to/groovy/embeddable/groovy-all-1.8.1.jar groovy.lang.GroovyShell test.groovy

Categories

Resources