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.
Related
I am trying to compile and run from the command line a simple java hello world program.
It compiles and runs successfully when I do:
> javac HelloWorld.java
> java HelloWorld
However, when I place a jar-dependency in this hello world program and the corresponding jar in the same directory, I run into a classpath issue.
I can compile with:
> javac -cp ./* HelloWorld.java
But I when I attempt to run while specifying the classpath to the jar, my HelloWorld class is not found.
> java -cp ./* HelloWorld
Error: Could not find or load main class HelloWorld
When classpath is ./*, the jar is found but not the class, and when classpath is ., the class is found but not the jar.
I also tried specifying both, but the main class is not found whenever I use a : in the classpath.
> java -cp "./*:." HelloWorld
Error: Could not find or load main class HelloWorld
How can I specify classpath for java to find both the jar and my class?
In powershell, one solution is to use ; instead of :.
I.e. this works in powershell:
> java -cp "./*;." HelloWorld
hello, world
The idea to try that came from an answer to this question:
I have a Java file that contains an external JAR and I want to run it. It is a simple project: https://github.com/eveningstar33/external-jar and I tested it with Java 8 and also with Java 11. The command to create the class file is javac -cp ./lib/* HelloWorld.java and it's working, but if I try to run the class using this command: java -cp ./lib/*:./ HelloWorld it doesn't work and I get this error message:
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
What should I do to run it? Thank you!
Use java -cp ./lib/* HelloWorld to run the class.
Update:
If the one given above doesn't work, please try with a "" i.e. use java -cp "./lib/*" HelloWorld to run the class.
Another update (tested):
Sorry for posting earlier solutions without testing them myself. Now, I have tested the following to be working:
java -cp .:lib/* HelloWorld
I had a fully functional Java program that I am now trying to put into a package instead of having it in the default package. All of the .java files are in the mymap package, and I am trying to run black box tests in my MyMapTest.java file. The problem is that I don't know how to properly run this in the command line now that I have it in a package. I got stuck trying to google for answers.
I can get it to compile:
javac -cp "../junit.jar" *.java;
But when I try to run it as I previously had...
java -cp .:../junit.jar org.junit.runner.JUnitCore MyMapTest
I get the error java.lang.NoClassDefFoundError: mymap/MyMap for all of my tests. I guess the issue is that somehow I am not properly including mymap, perhaps in the classpath?
Similarly, I also have white box tests in MyMapWhiteBoxTest.java, where this file is also in the mymap package. I cannot figure out how to properly run these tests either. Trying to run it the same way, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: MyMapWhiteBoxTest (wrong name: mymap/MyMapWhiteBoxTest)
For both cases, I have tried mymap/FILE or somehow also including the package in the classpath, but I'm definitely missing something.
If the main() is present in MyMapTest class then you can try the below command :
java -cp .:../junit.jar org.junit.runner.JUnitCore myMap.MyMapTest
After compilation: (javac -cp "../junit.jar" *.java;) what do you have in your current dir? You should have the file ./myMap/MyMapTest.class.
There are some encoding character in my code so I compile the code as follow:
javac -cp cayenne-2.0.4.jar Twokenize.java
It's ok. But when I run the program, it return an error:
java -cp cayenne-2.0.4.jar Twokenize test.txt
Here is an error:
Error: Could not find or load main class Twokenize
But when I just run:
java Twokenize test.txt
The program (main method) still runs smoothly util it meets the code that require external library.
Please help me. Thank you very much.
when you use -cp it looses current directory from classpath so you need to specify explicitly
Use
java -cp .:cayenne-2.0.4.jar Twokenize test.txt
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