I am executing 2 java classes which have StringUtils function. For this I have common-lang3.jar so I am giving command
`javac -cp common-lang3.jar *.java`
this time no error but after that
java -cp common-lang3.jar abc.class its giving
Could not find or load main class For linux server where I have to place jar files so that i can just give command javac *.java ? If in classpath java -classpath is not allowing me to edit. pls help
Change
java -cp common-lang3.jar abc.class
to (assuming abc has your main method)
java -cp common-lang3.jar:. abc
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
Can someone explain me why when I got multiple jar in my classpath, java can't find my main class ?
if I try
java -cp MyJar.jar package.App
I got something i wannt, currently an error since some libs are missing
But if I try
java -cp lib/*:MyJar.jar package.App
or even
java -cp lib/*:MyJar.jar:. package.App
I got the error
Could not find or load main class
On windows environment the classpath should be semicolon separated:classpath.html
So the command should be something like this:java -cp "lib/*;MyJar.jar;." package.App
If you are on Linux then try to enclose the complete path in quotes: java -cp "lib/*:MyJar.jar:." package.App
I have checked my java installations by compiling and running a HelloWorld program which works perfectly fine.
The problem comes when I compile my program with certain jar files which are located in the same directory as my java file. This is what I've done.
javac -cp "A.jar:B.jar" MyProg.java
This generates the class file MyProg.class successfully. Next when I run the following command, it gives this error error: could not load or find main class MyProg
The command is:
java -cp "A.jar:B.jar" MyProg
Next, I even tried next by moving the jars in a folder named lib and issued the following commands:
javac -cp "lib/*" MyProg.jar (works fine;generates a class file)
java -cp "lib/*" MyProg (issues the same error)
I am working on a linux machine. Can some one please resolve the error.
Add the current path to the classpath
java -cp .:A.jar:B.jar MyProg
I want to add some jar files to my binary at runtime, but I keep getting errors. I'm running this on a Windows machine. My code is in a directory called SeleniumTest.
Here is the command I used to compile:
javac SeleniumTest\src\com\src\test\First.java -d SeleniumTest\bin -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar
This worked successfully. However when I try to run this command:
java -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar SeleniumTest\bin com.src.test.First
I get a message:
Error: Could not find or load main class SeleniumTest\bin
My code, First.java exists in
SeleniumTest\bin\com\src\test
What am I doing wrong?
try this
java -cp "SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar;SeleniumTest\bin" com.src.test.First
try following
java -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar SeleniumTest\bin SeleniumTest\src\com\src\test\First