Java classpath issue '.' vs. './*' - java

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:

Related

Could Not Load Main Class (Java)

Probably this is a repeated question, but I just couldn't find an answer to what I am looking for! I am trying to compile and run a java class in a Unix box.
I have the class as:
package tmp.test;
import org.jasypt.registry.AlgorithmRegistry;
class Algo {
public static void main(String[] args) {
System.out.println(AlgorithmRegistry.getAllPBEAlgorithms());
}
}
The files are in the path /tmp/test/. Now I compile the class with the command:
javac -cp jasypt-1.9.3.jar Algo.java
The JAR file is in the same directory. It compiles just fine. But when I run the class file with the command:
java -cp jasypt-1.9.3.jar Algo
I get the error:
Error: Could not find or load main class Algo
I am executing all the commands from the path /tmp/test/.
I tried:
java -cp jasypt-1.9.3.jar tmp.test.Algo
and
java -cp jasypt-1.9.3.jar tmp/test/Algo
Both throw the same error.
I am not sure what I am doing wrong. At first I thought it was the problem of the access thing. So I changed everything using chmod to 777. Everything seems to be fine. Can you please let me know what I am missing here?
I am executing all the commands from the path /tmp/test/
That is the problem. You need to be one level above tmp, not somewhere inside. Then your command line
java -cp /path/to/jasypt-1.9.3.jar tmp.test.Algo
should work. If you insist in starting Java from the subdirectory inside your classpath, you can do this quite contrived thing:
java -cp /path/to/jasypt-1.9.3.jar:../.. tmp.test.Algo
tl;dr
Use the switch -d to compile and then use the fully qualified name of the class to run it.
Compile the class as follows:
javac -d . -cp jasypt-1.9.3.jar Algo.java
The switch, -d specifies where to place generated class files and . stands for the current directory.
Run the class as follows:
java -cp jasypt-1.9.3.jar tmp.test.Algo

How to include an external JAR in a directory within the Java classpath and run the Java class?

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

error: could not load or find main class xyz

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

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 ";"

Problems running a java program from the command line interface

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.

Categories

Resources