Here is the solution that finally worked as expected. When the '*' character is used in the classpath for my specific scenario, it skipped everything after the first path. Using the double quotes (") for each path separately and then using ";" as the delimiter is the solution.

Having any space before or after the semi-colon ";" will also not work

javac -verbose -classpath "C:\Program Files\lib\java\core\\*";"C:\Program Files\lib\java\core\locale\\*";"C:\Program Files\lib\java\modules\\*";"C:\Program Files\lib\java\modules\ext\\*" testClass.java
Answer from Srinivas on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › javaexamples › env_multiple_classpath.htm
How to set multiple classpath in Java
Following example demonstrates how to set multiple classpath. Multiple class paths are separated by a semicolon. c:> java -classpath C:\java\MyClasse1;C:\java\MyClass2 utility.testapp.main
🌐
Coderanch
coderanch.com › t › 537992 › java › multiple-classpath
specify multiple classpath [Solved] (Beginning Java forum at Coderanch)
my bad, I just try javac -classpath /Users/dolphin/Library/Tomcat/lib/servlet-api.jar:/Users/dolphin/Library/Tomcat/webapps/ROOT/classes SearchSinger.java and it works! Thanks a lot !!
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › windows › classpath.html
2 Setting the Class Path
April 21, 2026 - To find class files in the directory C:\java\MyClasses and classes in C:\java\OtherClasses, you would set the class path to the following. Note that the two paths are separated by a semicolon. java -classpath C:\java\MyClasses;C:\java\OtherClasses ... The order in which you specify multiple ...
🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › classpath.html
Setting the class path
C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses ... Note that the two paths are separated by a semicolon. The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path ...
🌐
2min2code
2min2code.com › articles › java_comamnd › multiple_paths_in_classpath
'java' command - Tutorial : How to add multiple paths in classpath
July 17, 2012 - To add multiple folders or jar files to the classpath, use the -cp or -classpath option with the path elements separted by semi-colon (on windows box only) or colon (on linux boxes).
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-set-multiple-classpath-in-java-in-windows
How to Set Multiple Classpath in Java in Windows? - GeeksforGeeks
December 8, 2020 - If the CLASSPATH already exists in System Variables, click on the Edit button then put a semicolon (;) at the end. Paste the Path of MySQL-Connector Java.jar file.
🌐
Delft Stack
delftstack.com › home › howto › java › java classpath
How to Set Classpath in Java | Delft Stack
February 2, 2024 - Use the colon as a separator for multiple paths. ... We can use the -classpath or the -cp option to set the classpath when compiling and running the class files. The code below demonstrates this.
🌐
UCSB
sites.cs.ucsb.edu › ~cappello › 50-2005-Winter › lectures › classpath.html
Setting the class path
Multiple path entries are separated by colons. The default class path is the current directory. Using the -classpath command-line option overrides that default: If you want to include the current directory in the search path, include "." in the settings. The class path tells the SDK tools and ...
Find elsewhere
Top answer
1 of 16
1340

Using Java 6 or later, the classpath option supports wildcards. Note the following:

  • Use straight quotes (")
  • Use *, not *.jar

Windows

java -cp "Test.jar;lib/*" my.package.MainClass

Unix

java -cp "Test.jar:lib/*" my.package.MainClass

This is similar to Windows, but uses : instead of ;. If you cannot use wildcards, bash allows the following syntax (where lib is the directory containing all the Java archive files):

java -cp "$(printf %s: lib/*.jar)"

(Note that using a classpath is incompatible with the -jar option. See also: Execute jar file with multiple classpath libraries from command prompt)

Understanding Wildcards

From the Classpath document:

Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/*;foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.

Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo, not in foo/bar, foo/baz, etc.

The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required then the JAR files can be enumerated explicitly in the class path.

Expansion of wildcards is done early, prior to the invocation of a program's main method, rather than late, during the class-loading process itself. Each element of the input class path containing a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory foo contains a.jar, b.jar, and c.jar, then the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar, and that string would be the value of the system property java.class.path.

The CLASSPATH environment variable is not treated any differently from the -classpath (or -cp) command-line option. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in the Class-Path jar-manifest header.

Note: due to a known bug in java 8, the windows examples must use a backslash preceding entries with a trailing asterisk: https://bugs.openjdk.java.net/browse/JDK-8131329

2 of 16
287

Under Windows this works:

java -cp "Test.jar;lib/*" my.package.MainClass

and this does not work:

java -cp "Test.jar;lib/*.jar" my.package.MainClass

Notice the *.jar, so the * wildcard should be used alone.


On Linux, the following works:

java -cp "Test.jar:lib/*" my.package.MainClass

The separators are colons instead of semicolons.

🌐
javaspring
javaspring.net › javaexamples › how_to_set_multiple_classpaths_in_java
How to Set Multiple Classpaths in Java — javaspring.net
To set multiple classpaths, separate each entry with a semicolon (;). For example: C:\projects\lib1;C:\projects\lib2;C:\projects\src ... Edit your shell startup script (e.g., .bashrc for Bash).
🌐
Princeton
cs.princeton.edu › courses › archive › fall97 › cs461 › jdkdocs › tooldocs › win32 › classpath.html
classpath - Environment Variables
The CLASSPATH environment variable ... of CLASSPATH. You can also use set in your startup file to specify a CLASSPATH at startup. The format is: set CLASSPATH=path1;path2 ......
🌐
Lenovo
lenovo.com › home
Java Classpath: Definition, Usage, Options & Best Practices | Lenovo US
Yes, classpath can include multiple directories or JAR files. It allows Java programs to locate classes scattered across different locations. Entries are separated by a semicolon (;) on Windows or a colon (:) on Unix-based systems. This flexibility is useful when projects rely on libraries ...
🌐
How to do in Java
howtodoinjava.com › home › java basics › java classpath
How to set CLASSPATH in Java - HowToDoInJava
February 23, 2023 - Apart from setting the classpath to the environment variable, you can pass an additional classpath to Java runtime while launching the application using –classpath option or –cp option.
🌐
Blogger
javarevisited.blogspot.com › 2012 › 10 › 5-ways-to-add-multiple-jar-to-classpath-java.html
5 ways to add multiple JAR in to Classpath in Java - Examples
August 31, 2021 - Here are few more important points about using Java 6 wildcard to include multiple JAR in classpath : 1) In order to include all JAR from a directory you need to use the wildcard * and not *.jar · 2) If you have JAR and class files in the same directory then you need to include each of them separately. Wildcard only matches JAR files and not classes. E.g. ... 4) One more important point is that wildcard to include all JAR is not honored in case if you are running a Java program with a JAR file and that have Class-Path attribute in the manifest file.
🌐
Medium
medium.com › javarevisited › back-to-the-basics-of-java-part-1-classpath-47cf3f834ff
Tutorial on how Java classpath works | Javarevisited
May 15, 2022 - > java -classpath ../lib/:. myprogram.Main Here is 1337 · This works because it will now be able to find Utils.class file using the first path.
🌐
Michigan State University
web.pa.msu.edu › reference › jdk-1.2.2-docs › tooldocs › solaris › classpath-linux.html
Setting the class path
To find class files in the directory /java/MyClasses as well as classes in /java/OtherClasses, you would set the class path to: % java -classpath /java/MyClasses:/java/OtherClasses ... Note that the two paths are separated by a colon. The order in which you specify multiple class path entries ...
🌐
Coderanch
coderanch.com › t › 476447 › certification › SOLVED-javac-cp-multiple-classpath
[SOLVED] javac -cp (multiple classpath) [Solved] (OCPJP forum at Coderanch)
December 27, 2009 - Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼ References : [Java.Boot] [JavaChamp] [JavaPrepare] ... in windows, use the delimiter for classpath is semicolin (;) So specify each path to be used, maybe use quotes if the path has spaces in it.
🌐
DEV Community
dev.to › awwsmm › easily-merge-multiple-java-classpath-arguments-1fj7
Easily Merge Multiple Java --classpath Arguments - DEV Community
June 21, 2019 - It automatically detects any variation of cp, classpath, and class-path flags in a series of command-line arguments, and concatenates all of the correct arguments to give you a nice, clean classpath. I hope it's useful! ... An alternative to the for loop is the while loop with the shift command. It is without indexes and much simpler. BTW: This script look like in Java.
🌐
Stack Overflow
stackoverflow.com › questions › 44074943 › java-use-classpath-to-include-multiple-jars-in-multiple-directories-all-in-one
Java use -classpath to include multiple jars in multiple directories all in one directory? - Stack Overflow
How can I use javac -cp to include foo.jar and far.jar without specifying every single directory? ... It's platform specific. See here for an example for specifying the classpath for the 'java' command: