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
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 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
I am new to Java, and having problems running my compiled code.
I have a file called AdditionApplication. I compile this line:
javac -cp * AdditionApplication.java
This produces a file in the current directory called AdditionApplication.class
When I try and run the program with the code:
java -cp * AdditionApplication
It gives the error Error: Could not find or load main class AdditionApplication
What could be a cause of such behavior?
UPDATE
So my code requires a jar file to be included in the initial compilation of the file. This seemed to only work if I had javac - cp * AdditionApplication.java, or if I specified the full path to the jar file.
When I switch over to us a . in 'java -cp . AdditionApplication', I get the error NoClassDefFoundError - which I gather occurs when the class which was initial reference during compilation is no longer available - why would that be the case?
The classpath should be the directory with the files (not * unless they're jar files). Assuming you have a class file (and based on your other command you do), you add that folder to the classpath. Something like,
java -cp . AdditionApplication
Since you have a jar as well, you could do (on *NIX systems)
java -cp .:* AdditionApplication
or on Windows
java -cp .;* AdditionApplication
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
I created a java program in Eclipse. When I run the program in Eclipse ("run as -> Java Application") the program runs fine and I have the correct output. However, when I try to run the program in the command line interface I got this error:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld/HelloWorld)
Could not find the main class: HelloWorld. Program will exit.
The class file is in directory bin and I try to run it with the command:
java HelloWorld
Since your class is in the package helloworld you should run it like this:
java helloworld.HelloWorld
Also make sure "." is on your classpath.
I try to compile it with the command:
java HelloWorld
TO compile a java program you should use javac command like
javac Helloworld.java
Are you sure that the directory where your classes are is in the classpath? Typically, in your project directory, the "classes" or "lib" directory.
If you are running from that directory, you could try adding ".".
See the -cp parameter of java runtime executable.