Fundi
Fundi

Reputation: 131

Running Java command including all JARs in current folder

I have just shifted back from an IDE to Notepad to write a Java program. The program is using 20 JARs. I compiled successfully. When I decided to run the Java class file using

java -cp ".\\*" MyProgram

it was giving the standard error "Couldn't find or load main class....".

I was confused because when I used to run the java command with all files in an existing folder, it would just get those JARs as the current folder is already in the classpath. As the program is running from the current folder, I tried using -cp "." to include it explicitly in the classpath but that didn't work either.

Finally I was able to run the program with this command:

java -cp ".\\*;." MyProgram.java

I am asking this question to understand the actual logic behind Java's classpath.

Correct me if I am wrong, but I think that the JAR is just a standard archive in which all the packages are encapsulated in respective folders. If all the JARs are in my current folder including my main class file then why can't I run it with:

java -cp "." MyProgram 

or simply:

java MyProgram

If the problem is with the multiple JAR files to include and that's why we used ".\\*" to include all the JARs in the classpath, then why do we have to explicitly include the current folder again in the classpath using:

java ".\\*;." MyProgram

Upvotes: 5

Views: 7517

Answers (4)

tiktak
tiktak

Reputation: 1230

You've answered your own question, sort of.

. means that it will look for .class files in the current directory.

JARs act just like a directory. So to have the abc.jar "directory" you would specify abc.jar in your classpath.

If you need both the .class files present in the current directory, and the .class files packaged into JARs found in the current directory, you would have the following classpath: -cp ".:*.jar

All the answers here are telling you to use the wildcard without extension (* or ./*) but this is a bad practice, you don't want Java to go look into irrelevant files, so specify the extension: *.jar.

Upvotes: 0

Henry
Henry

Reputation: 43798

The class path is a list of jar files and directories containing the classes and resources of your program. Mentioning a jar file adds its contents to the class path.

"./*" will get you only the jar files in the current directory, "." adds the current directory to the class path. This allows to access all classes (and the jar files as raw file resources but not its contents, i.e. the classes contained in them).

If you need both in the class path, you have to specify both.

Upvotes: 1

Mohd Yasin
Mohd Yasin

Reputation: 483

To include all jar required to run your program in command prompt use wildcard *:

java -classpath E:\lib\* HelloWorld

You are using "." that defines current directory and "./*" defines all files in current directory.

Upvotes: 1

Dev
Dev

Reputation: 6786

"." means current directory not files in the directory

"./*" means all files in current directory.

So you want to use all jars in current directory so 2nd will work

Upvotes: 0

Related Questions