如何从 .jar 存档运行 java 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12801195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run java class from .jar archive?
提问by johnny-b-goode
Need to run particular java class from jar package through unix console. Is it possible? Thanks
需要通过 unix 控制台从 jar 包运行特定的 java 类。是否可以?谢谢
回答by Mesop
It's not possible to call a specific method of a class from a terminal.
无法从终端调用类的特定方法。
You can only call the main method with java -cp JarFile.jar package.ClassName
您只能调用 main 方法 java -cp JarFile.jar package.ClassName
Or, if your jar contains a manifest file, then you can do: java -jar pJarFile.jar
.
或者,如果你的罐子包含清单文件,那么你可以这样做:java -jar pJarFile.jar
。
回答by Santosh
For running a jar file you can have eitherof following approach. Both of them require that you know the fully qualified name of the class where main(String[] arg)
method is written.
要运行 jar 文件,您可以采用以下任一方法。它们都要求您知道main(String[] arg)
编写方法的类的完全限定名称。
Say your class containing main method is com.myclass.MainClass
假设你的包含 main 方法的类是 com.myclass.MainClass
You can run the jar directly. Keep the jar file at the location from where you are running this command
java -cp yourjarfile.jar com.myclass.MainClass
Create a manifest
manifest.mf
file with following contentMain-class: com.myclass.MainClass
你可以直接运行jar。将 jar 文件保存在您运行此命令的位置
java -cp yourjarfile.jar com.myclass.MainClass
创建
manifest.mf
具有以下内容的清单文件Main-class: com.myclass.MainClass
now create the jar file as follows
现在创建jar文件如下
jar -cmf manifest.mf yourjarfile.jar <your class file location>
Run this jar with the following command
使用以下命令运行此 jar
java -jar yourjarfile.jar
回答by Mudassir Hasan
Use
利用
java -cp myJar.jar myClass
The -cpoption is used when setting up the classpath manually ie give full path of location of jar file and then run command
该-cp设置CLASSPATH时手动即给jar文件的位置的完整路径选项用来然后运行命令
Eg.
例如。
C:> java -cp C:\java\MyClasses\myJar.jar myClass
回答by dunn less
you can extract the specific class file by doing:
您可以通过执行以下操作来提取特定的类文件:
jar xf MyProgram.jar theclass.class
it will end up in your current directory
它将最终出现在您当前的目录中
then just run the classfile
然后只需运行类文件
java theclass
回答by Sjoerd
Yes, use the -jar parameter:
是的,使用 -jar 参数:
java -jar MyProgram.jar