If you have a jar file called myJar.jar
located in /myfolder and you want to use the class called myClass
from it, how do you go about doing it from the command line?
I thought it would be to go into the directory and say java -cp myJar.jar.myClass
but that isn't working. Any help would be appreciated.
Use java -cp myjar.jar com.mypackage.myClass
.
If the class is not in a package then simply java -cp myjar.jar myClass
.
If you are not within the directory where myJar.jar
is located, then you can do:
On Unix or Linux platforms:
java -cp /location_of_jar/myjar.jar com.mypackage.myClass
On Windows:
java -cp c:\\location_of_jar\\myjar.jar com.mypackage.myClass
You want:
java -cp myJar.jar myClass
The Documentation gives the following example:
C:> java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
There are two types of JAR files available in Java:
Runnable/Executable jar file which contains manifest file. To run a Runnable jar you can use java -jar fileName.jar
or java -jar -classpath abc.jar fileName.jar
Simple jar file that does not contain a manifest file so you simply run your main class by giving its path java -cp ./fileName.jar MainClass
Assuming you are in the directory where myJar.jar
file is and that myClass
has a public static void main()
method on it:
You use the following command line:
java -cp ./myJar.jar myClass
Where:
myJar.jar
is in the current path , note that .
isn't in the current path on most systems. A fully qualified path is preferred here as well.
myClass
is a fully qualified package path to the class, the example assumes that myClass
is in the default package which is bad practice, if it is in a nested package it would be com.mycompany.mycode.myClass
.
这是执行的正确方法.jar
,并且,无论一类.jar
应该有main()
和下面是参数吧:
java -DLB="uk" -DType="CLIENT_IND" -jar com.fbi.rrm.rrm-batchy-1.5.jar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.