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:
- include all jar files from the
libdirectory into the manifest (you can use relative paths there) - Specify everything (including your jar) on the commandline using
-cp:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
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:
- include all jar files from the
libdirectory into the manifest (you can use relative paths there) - Specify everything (including your jar) on the commandline using
-cp:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
Run a jar file and specify a class path like this:
java -cp <jar_name.jar:libs/*> com.test.App
jar_name.jar is the full name of the JAR you want to execute
libs/* is a path to your dependency JARs
com.test.App is the fully qualified name of the class from the JAR that has the main(String[]) method
The jar and dependent jar should have execute permissions.
How to include jar files with java file and compile in command prompt - Stack Overflow
How to use java and javac commands with external jar library?
java - Including jars in classpath on commandline (javac or apt) - Stack Overflow
java - Setting the classpath for JAR files - Stack Overflow
Videos
You can include your jar files in the "javac" command using the "-cp" option.
javac -cp ".:/home/path/mail.jar:/home/path/servlet.jar;" MyJavaFile.java
Instead of "-cp" you could also use "-classpath"
javac -classpath ".:/home/path/mail.jar:/home/path/servlet.jar:" MyJavaFile.java
You could including the jars every time you compile by setting the environment variable "CLASSPATH" correctly. The environment variable will store the path where the jars and classes that needs to be used for compiling/executing any java file. You will not have to include the jars individually every time you compile you file.
Different machines have different methods to set the classpath as an environment variable. The commands for Windows, Linux, etc are different.
You can find more details in this blog.
http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html
Please try on Linux
javac -cp jarfile source file
EXAMPLE :-
javac -cp .:/jars/* com/template/*.java
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?
Try the following:
java -cp jar1:jar2:jar3:dir1:. HelloWorld
In Windows use ;
java -cp jar1;jar2;jar3;dir1;. HelloWorld
The default classpath (unless there is a CLASSPATH environment variable) is the current directory so if you redefine it, make sure you're adding the current directory (.) to the classpath as I have done.
In windows:
java -cp C:/.../jardir1/*;C:/.../jardir2/* class_with_main_method
make sure that the class with the main function is in one of the included jars
while setting the classpath a single dot (.) means current directory. As you jar files are in current directory, you just need to go to your current directory using cd command in DOS prompt, then use
set classpath = .;filename.jar;another filename.jar
Here . represents current directory and semicolon separates each classpaths.
You can even set classpath of more than one jar files using wild card character * which can be read as all.
You need something like
java -classpath lib/foo.jar:. com.company.Program
you can also use wildcards since java 6. see here
so the above becomes
java -classpath lib/*:. com.company.Program
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 .;
Your problem is min separator you are using. Separator ; is for windows. On Unix systems you should use : instead:
java -classpath "bin:mysql-connector-java-5.1.19-bin.jar" MyProgram
Use a ':' to separate your entries on Unix systems:
java -classpath "bin:mysql-connector-java-5.1.19-bin.jar" MyProgram
java -cp bin:mysql-connector-java-5.1.19-bin.jar MyProgram
Eclipse converts it automatically.