运行Jar文件中的类
如果您有一个名为 myJar.jar
的 jar 文件,位于 /myfolder 中,并且您想使用其中名为 myClass
的类,您该如何做可以从命令行执行此操作吗?
我认为应该进入目录并输入 java -cp myJar.jar.myClass
但这不起作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 java -cp myjar.jar com.mypackage.myClass 。
如果该类不在包中,则只需
java -cp myjar.jar myClass
。如果您不在
myJar.jar
所在的目录中,那么您可以执行以下操作:在 Unix 或 Linux 平台上:
java -cp /location_of_jar/myjar.jar com.mypackage.myClass
在 Windows 上:
java -cp c:\location_of_jar\myjar.jar com.mypackage.myClass
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:
The Documentation gives the following example:
Java 中有两种类型的 JAR 文件:
包含清单文件的可运行/可执行 jar 文件。
要运行 Runnable jar,您可以使用
java -jar fileName.jar
或java -jar -classpath abc.jar fileName.jar
简单的 jar 文件,不包含清单文件,因此您只需提供其路径 java -cp ./fileName.jar MainClass
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
orjava -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
假设您位于
myJar.jar
文件所在的目录中,并且myClass
有一个public static void main()
方法:您使用以下命令行:
其中:
myJar.jar
位于当前路径中,请注意.
不在当前路径中在大多数系统上。这里也首选完全限定路径。myClass
是类的完全限定包路径,该示例假设myClass
位于默认包中strong> 这是不好的做法,如果它在嵌套包中,它将是com.mycompany.mycode.myClass
。Assuming you are in the directory where
myJar.jar
file is and thatmyClass
has apublic static void main()
method on it:You use the following command line:
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 thatmyClass
is in the default package which is bad practice, if it is in a nested package it would becom.mycompany.mycode.myClass
.这是执行
.jar
的正确方法,并且.jar
中的任何一个类都应该具有main()
,以下是它的参数:This is the right way to execute a
.jar
, and whatever one class in that.jar
should havemain()
and the following are the parameters to it :