When you specify -jar then the -cp parameter will be ignored.

From the documentation:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)

You have two options:

  1. include all jar files from the lib directory into the manifest (you can use relative paths there)
  2. Specify everything (including your jar) on the commandline using -cp:
    java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
Answer from user330315 on Stack Overflow
🌐
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 ...
Discussions

How to include jar files with java file and compile in command prompt - Stack Overflow
I have 3 jar files and a .java file that depends on these jar files. How do I compile the .java file with these jar files using a command prompt? More on stackoverflow.com
🌐 stackoverflow.com
How to use java and javac commands with external jar library?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
5
3
August 17, 2023
java - Including jars in classpath on commandline (javac or apt) - Stack Overflow
trying to run this program. I think that to setup all of the web service stuff I need to run apt. (Although using javac I am having the same issue). I think what I am getting is compile errors. ( More on stackoverflow.com
🌐 stackoverflow.com
java - Setting the classpath for JAR files - Stack Overflow
I have recently just created Java project using Eclipse that requires 2 JAR files (phiget21.jar and the mysql.jar) Everything works fine when running the programme in Eclipse, and I have noticed t... More on stackoverflow.com
🌐 stackoverflow.com
🌐
How to do in Java
howtodoinjava.com › home › java basics › java classpath
How to set CLASSPATH in Java - HowToDoInJava
February 23, 2023 - Use -classpath argument to set classpath from command prompt/console. Use the following command to set the classpath for different requirements. Let’s say we have a folder named dependency where JAR files and other classes are placed.
🌐
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 - This option is viable when we are passing -classpath option while running our java program like java -classpath $(CLASSPATH) Main. In this case, CLASSPATH shell variable contains the list of Jar file which is required by the application.
🌐
HowToDoInJava
howtodoinjava.com › home › java examples › java – set classpath from command line
Java - Set Classpath from Command Line
January 25, 2022 - Learn to use the -classpath or -cp option to set the Java classpath from the command prompt in Windows and Linux OS. 1. Java Classpath The classpath is the list of directory locations that the Java runtime environment searches for the classes ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › windows › classpath.html
2 Setting the Class Path
April 21, 2026 - 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, the command to use a class library that is in a JAR file as follows: java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
Find elsewhere
🌐
Reddit
reddit.com › r/learnjava › how to use java and javac commands with external jar library?
r/learnjava on Reddit: How to use java and javac commands with external jar library?
August 17, 2023 -

I'm learning the basics of dependencies and how it all works under the hood. For this purpose I've created basic Main.java file that relies on external library.jar file. They're both in the same directory.

I compiled my Main.java file with javac -cp ".\library.jar" Main.java. But, when I try to run it with java -cp ".\library.jar" Main I get the following error:

Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main

I also tried this variation java -cp ".\library.jar";. Main and console prints the usage for java command.

What am I missing?

Top answer
1 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 2
-1
Try this, what ChatGPT says: It looks like you're almost there, but there might be a small issue with how you're specifying the classpath and the main class when running your Java program. Let's break it down step by step: **Compilation**: You've compiled your `Main.java` using the correct classpath for `library.jar`: ``` javac -cp ".\library.jar" Main.java ``` **Running**: Now, when you're trying to run the compiled class `Main`, you need to specify the classpath for both your compiled classes and the external `library.jar`: On Windows, you should use a semicolon (`;`) as the classpath separator, not a colon. It seems like you've used a period (`.`) instead of a semicolon in your command. Also, you should provide the fully qualified name of the main class (including the package name, if any). If `Main` is in the default package, you can use just `Main` as the main class name. Here's how you can run your program correctly: ``` java -cp ".\;library.jar" Main ``` This assumes that both `Main.class` (generated after compilation) and `library.jar` are in the same directory. Make sure you're running the above `java` command from the same directory where `Main.class` and `library.jar` are located. Also, ensure that the `Main` class has a `main` method defined within it. If you're still encountering issues, please provide more details about the structure of your directory, any packages you might be using, and the contents of your `Main.java` file.
🌐
Drew University
users.drew.edu › bburd › JavaForDummies6 › classpath.pdf pdf
1 Chapter 2 Setting your Classpath
classpath for one invocation of the javac or java command. To do this, you type ... The set command holds for only one copy of the command prompt window.
🌐
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 - First, if we’re launching our program from the command line, it can make sense to specify our JAR dependencies as part of the command: java -cp /path/to/jar/file.jar com.example.MyClass
🌐
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.
🌐
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 - The Class-Path option takes the highest priorities and overrides both the CLASSPATH environment variable and the –classpath command-line option. This is also a good place to include all JAR files required by Java applications. 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.
🌐
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)
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.
🌐
Reddit
reddit.com › r/learnprogramming › java : how to use a -classpath in the command prompt.
r/learnprogramming on Reddit: Java : How to use a -classpath in the command prompt.
August 6, 2015 -

So I have two different .java files in two different directories. One contains the main method which calls the other one through its method name. . This is in the directory

E:\JavaWorld\TardyCoder\temp1

Here is my program which contains the main method with the fileName Lemons.java

public class Lemons{
public static void main(String args[]){

Sorra sorraObject = new Sorra();
sorraObject.testName();	
 }
 }

My another program is in the directory

E:\JavaWorld\TardyCoder\temp2

Here is my program Sorra.java which doesn't contain the main method.

 class Sorra{
public void testName(){
System.out.println("My name is Khan.");
 }
 }

I used -sourcepath to create two .class files like this:

   E:\JavaWorld\TardyCoder\temp1>javac -sourcepath E:\JavaWorld\TardyCoder\temp2 Lemons.java

Everything worked perfectly and i got two .class files in their respective directories.

Now I tried to use the -classpath to get the output like this:

E:\JavaWorld\TardyCoder\temp1>java -classpath .; E:\JavaWorld\TardyCoder\temp2 Lemons

But I'm getting an error :(

Error: Could not find or load main class E:\JavaWorld\TardyCoder\temp2

Please explain the right way to use -classpath in command prompt.

Edit: Got the answer. There should be no space after .;

Top answer
1 of 2
2
http://www.ibm.com/developerworks/library/j-classpath-windows/ Best article about the topic. Please include the error next time.
2 of 2
1
You need to put 2 line breaks to make paragraphs in your post. To format code blocks, indent with 4 spaces. Then your post becomes more readable and you'll get more help: So I have two different .java files in two different directories. One contains the main method which calls the other one through its method name. I used -sourcepath to create two .class files like this: E:\... javac -sourcepath E:\... FileName.java Now I tried to use the -classpath to get the output like this: E:\... java -classpath E:\.. FileName But I'm getting an error :( Please explain the way to use -classpath in command prompt. First things first, you should always mention errors when ever you ask for help. Anyway, I assume this isn't what you're actually executing, since it's not valid syntax. I assume that you must have removed paths and that the leading path is actually part of the prompt string (most people use $ to show the prompt string). If that's not the case, then this would be the underlying problem. It's noteworthy that you probably don't want to be using absolute paths here. You almost always want relative paths (eg, if you're in /foo/bar/baz and want to refer to a file with the path /foo/bar/tor/gat.png, you'd use the relative path ../tor/gat.png -- .. means go up a folder). Anyway, when you've compiled your Java files into class files, their location is the class path that you'd want. In 99% of projects, you don't need to set the class path because it defaults to including the current directory, which is what you'd want. Eg, if your project contains two files, A.java and B.java in the default package, you'd compile with javac A.java B.java # Or use `javac *.java` for simplicity which will result in the files A.class and B.class (and possibly others for inner classes, etc) appearing in the directory. You'd then merely have to point the JVM at the class with the entry point with java A Which requires no classpath since the class files are in the default package and the current working directory, and thus the JVM is able to find them. Now on the other hand, if your project had the files in foo/A.java and foo/B.java, where foo is the package they're in, you'd have to run the files from the base directory (not in the packages) with java foo.A # Note the dot instead of a slash And if foo.A needs foo.B, then the JVM will be searching for foo/B.class. So how does the JVM know where to look? That's what the classpath is for. In the cases above, we didn't have to set the class path because we made sure to work from the base directory from which all our packages are in. Usually you'd set the classpath for libraries, not for your own code. Having to set it for your own code may be indicative of you doing something wrong. If you get the general gist of what I'm saying here, perhaps you'll be able to figure out how to get things working (experiment a bit, if needed). If you're still stuck, elaborate on the exact paths you're using, the locations of files, the error message you're getting, and the packages that files are in.
🌐
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
🌐
Edureka
edureka.co › blog › set-java-classpath
How To Set Classpath In Java | Java Path And Classpath | Edureka
June 19, 2023 - It commonly contains paths to Jar files with OS path separator in windows it is ‘;’. If you want to specify classpath while running any program using command line you can use Java -cp “some.jar;someOther.jar” com.edureka.HelloWorld .