5

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
1
  • docs.oracle.com/javase/8/docs/technotes/tools/windows/…. See section "Class path wild cards." Quote: "Class path entries can contain the base name wildcard character (*), which is considered equivalent to specifying a list of all of the files in the directory with the extension .jar or .JAR. For example, the class path entry mydir/* specifies all JAR files in the directory named mydir. A class path entry consisting of * expands to a list of all the jar files in the current directory." So really you only need cp "*;." Commented Oct 10, 2017 at 6:40

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

2 Comments

No, the correct syntax is indeed .\* (or ./* in Unix, or better still just *). See Oracle docs: "Class path entries can contain the base name wildcard character (*), which is considered equivalent to specifying a list of all of the files in the directory with the extension .jar or .JAR. For example, the class path entry mydir/* specifies all JAR files in the directory named mydir. A class path entry consisting of * expands to a list of all the jar files in the current directory." docs.oracle.com/javase/8/docs/technotes/tools/windows/…
None of this works for me on Mac (I know the OP is on Windows but I see no reason it shouldn't work). /usr/local/Cellar/openjdk@17/17.0.4.1_1/bin/java -cp ".:*.jar" App.java. I get compile errors that don't appear if I explicitly specify the jar : -cp webdrivermanager-5.3.0-fat.jar
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.