[英]Run class in Jar file
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? 如果你有一个名为
myJar.jar
的jar文件位于/ myfolder中,并且你想从中使用名为myClass
的类,那么你如何从命令行开始呢?
I thought it would be to go into the directory and say java -cp myJar.jar.myClass
but that isn't working. 我以为会进入目录并说
java -cp myJar.jar.myClass
但这不起作用。 Any help would be appreciated. 任何帮助,将不胜感激。
Use java -cp myjar.jar com.mypackage.myClass
. 使用
java -cp myjar.jar com.mypackage.myClass
。
If the class is not in a package then simply java -cp myjar.jar myClass
. 如果类不在包中,那么只需
java -cp myjar.jar myClass
。
If you are not within the directory where myJar.jar
is located, then you can do: 如果您不在
myJar.jar
所在的目录中,那么您可以:
On Unix or Linux platforms: 在Unix或Linux平台上:
java -cp /location_of_jar/myjar.jar com.mypackage.myClass
On Windows: 在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: Java中有两种类型的JAR文件:
Runnable/Executable jar file which contains manifest file. 包含清单文件的Runnable / Executable jar文件。 To run a Runnable jar you can use
java -jar fileName.jar
or java -jar -classpath abc.jar fileName.jar
要运行Runnable jar,可以使用
java -jar fileName.jar
或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
简单的jar文件,不包含清单文件,因此您只需通过提供其路径
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: 假设您位于
myJar.jar
文件所在的目录中,并且myClass
有一个public static void main()
方法:
You use the following command line: 您使用以下命令行:
java -cp ./myJar.jar myClass
Where: 哪里:
myJar.jar
is in the current path , note that .
myJar.jar
在当前路径中 ,请注意.
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
. myClass
是该类的完全限定的包路径 ,该示例假定myClass
位于默认包中 ,这是不好的做法,如果它在嵌套包中则为com.mycompany.mycode.myClass
。
这是执行的正确方法.jar
,并且,无论一类.jar
应该有main()
和下面是参数吧:
java -DLB="uk" -DType="CLIENT_IND" -jar com.fbi.rrm.rrm-batchy-1.5.jar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.