๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ core java
Include Jars In Java Classpath Example - Java Code Geeks
February 13, 2025 - You can specify JAR files on the command line using the -cp (or -classpath) option when executing a Java program. This method is useful when running Java programs manually without using an Integrated Development Environment (IDE) or build tools. For example, suppose we have a JAR file named example.jar containing a class com.example.HelloWorld.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ deployment โ€บ jar โ€บ downman.html
Adding Classes to the JAR File's Classpath (The Javaโ„ข Tutorials > Deployment > Packaging Programs in JAR Files)
The following is an example of the Class-Path manifest attribute: Class-Path: servlet.jar infobus.jar acme/beans.jar images/ By using the Class-Path attribute in your application's JAR file manifest, you can avoid having to specify a long -classpath flag when launching java to run your application.
Discussions

command line - Including all the jars in a directory within the Java classpath - Stack Overflow
This is similar to Windows, but ... all the Java archive files): ... (Note that using a classpath is incompatible with the -jar option. See also: Execute jar file with multiple classpath libraries from command prompt) ... 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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Classpath
So you can compile your Cool.java class on disk D for example, and using the -classpath you can run it anywhere. My advice is however to finish Java Track first and learn to use dependency management. Tools like Gradle and Maven will keep things organized for you, even if you use external jars More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
1
January 27, 2017
Understanding Java classpath.
A .jar file is really just a zipfile with a bunch of classes inside, these classes are generally inside packages (namespaces) that match a directory structure. So a jar is really just a directory tree with classes. The classpath is just a complete directory tree of classes that gets formed from the standard library (java.util.List, etc.), the librarary jar files (CoolFramework.jar for example), and your own source code. This 'classpath' is really just an in-memory directory tree. So if you use org.javaguy.coolframework.SomeClass in your code, the JVM knows to look in ./org/javaguy/coolframework for a SomeClass.class file and load that. More on reddit.com
๐ŸŒ r/learnprogramming
4
2
January 13, 2021
Call "java -jar MyFile.jar" with additional classpath option - Stack Overflow
With this in place, java -jar MyProgram.jar will work correctly, and will include all the libs JAR files on the classpath as well. ... Sign up to request clarification or add additional context in comments. ... Adding to above, Or think of adding the required jar entries in MANIFEST.MF file. 2013-04-10T16:13:55.81Z+00:00 ... @HimanshuBhardwaj indeed, I've added an example ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Medium
medium.com โ€บ javarevisited โ€บ back-to-the-basics-of-java-part-1-classpath-47cf3f834ff
Tutorial on how Java classpath works | Javarevisited
May 15, 2022 - In other words, I thought it was ... with the java cli tools and going back to basics. Now letโ€™s get down to it. In this part I will explain what the classpath is and how to use it correctly (and incorrectly). The classpath is simply a list of directories, JAR files, and ZIP ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ jvm โ€บ ways to add jars to classpath in java
Ways to Add JARs to Classpath in Java | Baeldung
July 19, 2025 - Manifest-Version: 1.0 Class-Path: lib/jar1.jar lib/jar2.jar Main-Class: com.example.MainClass ยท Then we add this manifest when creating our application JAR: jar cvfm app.jar MANIFEST.MF -C /path/to/classes . ... This includes lib/jar1.jar and lib/jar2.jar on the classpath. Itโ€™s important to note that the Class-Path option takes precedence over the CLASSPATH environment variable as well as the โ€“classpath command-line option. Adding JARs in the lib/ext directory of the Java installation is a legacy mechanism, where JAR files placed in it are automatically added to the classpath.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java basics โ€บ java classpath
How to set CLASSPATH in Java - HowToDoInJava
February 23, 2023 - There is a subtle but serious bug ... all classes and not just for dependant classes. The first statement โ€œ$ javac โ€“classpath C:\dependency\framework.jar MyApp.Javaโ€ compiles fine and places the MyApp.class file in the current ...
๐ŸŒ
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 - From Java 1.6+ onwards you can use a wildcard to include all jars in a directory into the set classpath or provide it to the Java program directly using -classpath command-line option. Following Java, the command example shows how to add multiple JAR into classpath using Java 6 wildcard method.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ technotes โ€บ tools โ€บ windows โ€บ classpath.html
2 Setting the Class Path
April 21, 2026 - For example, suppose you want the ... contains C:\java\MyClasses. To run that application, you could use the following java command: java -classpath C:\java\MyClasses utility.myapp.Cool...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ โ€บ 7 โ€บ docs โ€บ technotes โ€บ tools โ€บ windows โ€บ classpath.html
Setting the class path
But when classes are stored in an archive file (a .zip or .jar file) the class path entry is the path to and including the .zip or .jar file. For example, to use a class library that is in a .jar file, the command would look something like this: C:> java -classpath C:\java\MyClasses\myclasses.jar ...
Find elsewhere
๐ŸŒ
Chilkat
chilkatsoft.com โ€บ java-classpath-Windows.asp
Java JAR Archives and classpath on Windows
javac -classpath ".;C:/chilkatJava/chilkat.jar" Test.java java -classpath ".;C:/chilkatJava/chilkat.jar" Test
๐ŸŒ
Chilkat
chilkatsoft.com โ€บ java-classpath-Linux.asp
Java JAR Archives and classpath on Linux
javac -classpath ".:/chilkatJava/chilkat.jar" Test.java java -classpath ".:/chilkatJava/chilkat.jar" Test
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.

๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ classpath
Classpath (Example) | Treehouse Community
January 27, 2017 - If the path to that directory is C:\java\MyClasses\utility\myapp, you would set the class path so that it contains C:\java\MyClasses. To run that app, you could use the following JVM command: C:> java -classpath C:\java\MyClasses utility.myapp.Cool
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Classpath
Classpath - Wikipedia
May 30, 2026 - When overriding however, it is advised to include the current folder "." into the classpath in the case when loading classes from current folder is desired. The same applies not only to java launcher but also to javac, the java compiler. If a program uses a supporting library enclosed in a Jar file ...
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ java examples โ€บ java โ€“ set classpath from command line
Java - Set Classpath from Command Line
January 25, 2022 - Use set CLASSPATH command initially, and then run Java application or tool in the same command line window. โ€œ It will search the classes/resources in mentioned classpath locations. Classpath entries that are neither directories nor archives (.zip or .jar files) nor * are ignored. ... The above examples ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-add-jar-file-to-classpath-in-java
How to set classpath when class files are in .jar file using Java
July 21, 2023 - Following example demonstrates how to set class path when classes are stored in a .jar or .zip file. c:> java -classpath C:\java\myclasses.jar utility.testapp.main
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ understanding java classpath.
r/learnprogramming on Reddit: Understanding Java classpath.
January 13, 2021 -

I was surfing stackoverflow, trying to better understand what the classpath is all about and came upon this question/answer. It mostly makes sense, but there are two slightly confusing paragraphs in the accepted answer that go:

First, let's suppose that MyClass is something you built as part of your project, and it is in a directory in your project called output. The .class file would be at output/org/javaguy/coolframework/MyClass.class (along with every other file in that package). In order to get to that file, your path would simply need to contain the folder 'output', not the whole package structure, since your import statement provides all that information to the VM.

Now let's suppose that you bundle CoolFramework up into a .jar file, and put that CoolFramework.jar into a lib directory in your project. You would now need to put lib/CoolFramework.jar into your classpath. The VM will look inside the jar file for the org/javaguy/coolframework part, and find your class.

I don't understand the two sentences that say:

You would now need to put lib/CoolFramework.jar into your classpath. The VM will look inside the jar file for the org/javaguy/coolframework part, and find your class.

Why would the VM "look inside the jar file for the org/javaguy/coolframework part"? Why does that structure exist within the jar file? The answer mentions that you bundle "CoolFramework up into a .jar file", so why would that include the org/javaguy/coolframework hierarchy? Isn't CoolFrameWork just a single directory?

Top answer
1 of 4
178

You use either -jar or -cp, you can't combine the two. If you want to put additional JARs on the classpath then you should either put them in the main JAR's manifest and then use java -jar or you put the full classpath (including the main JAR and its dependencies) in -cp and name the main class explicitly on the command line

java -cp 'MyProgram.jar:libs/*' main.Main

(I'm using the dir/* syntax that tells the java command to add all .jar files from a particular directory to the classpath. Note that the * must be protected from expansion by the shell, which is why I've used single quotes.)

You mention that you're using Ant so for the alternative manifest approach, you can use ant's <manifestclasspath> task after copying the dependencies but before building the JAR.

<manifestclasspath property="myprogram.manifest.classpath" jarfile="MyProgram.jar">
  <classpath>
    <fileset dir="libs" includes="*.jar" />
  </classpath>
</manifestclasspath>

<jar destfile="MyProgram.jar" basedir="classes">
  <manifest>
    <attribute name="Main-Class" value="main.Main" />
    <attribute name="Class-Path" value="${myprogram.manifest.classpath}" />
  </manifest>
</jar>

With this in place, java -jar MyProgram.jar will work correctly, and will include all the libs JAR files on the classpath as well.

2 of 4
30

When the -jar option is used the -cp option is ignored. The only way to set the classpath is using manifest file in the jar.

It is easier to just use the -cp option, add your jar file to that, then explicitly call the main class.

Also, assuming the /home/user/java/MyProgram/jar/libs folder contains jar files (as opposed to class files) this won't work. You cannot specify a folder of jar file but must specify each jar file individually in the classpath (it is worth writing a simple shell script to do this for you if there are a significant number of jars).

๐ŸŒ
UCSB
sites.cs.ucsb.edu โ€บ ~cappello โ€บ 50-2005-Winter โ€บ lectures โ€บ classpath.html
Setting the class path
For example, to use a class library that is in a .jar file, the command would look something like this: % java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-add-jar-file-to-classpath-in-java
How to Add JAR file to Classpath in Java? - GeeksforGeeks
August 7, 2021 - Wildcard is included in all JAR ... or -classpath option ยท Command 5: Adding JAR in ext directory example be it 'C:\Program Files\Java\jdk1.6.0\jre\lib\ext'...