简体   繁体   English

无法使用java -jar命令执行jar文件

[英]Failing to execute the jar file using java -jar command

I have created a jar file usign maven2 build. 我创建了一个jar文件usign maven2 build。 I am trying to run that jar file using the command: 我试图使用命令运行该jar文件:

java -jar sample.jar com.app.Test

Test being the class which is having the main method. Test是具有主要方法的类。 But i am getting this exception: 但我得到这个例外:

Exception in thread "main" java.lang.NullPointerException
        at sun.launcher.LauncherHelper.getMainClassFromJar(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Can any one help me to solve this exception and run the jar file? 任何人都可以帮我解决这个异常并运行jar文件吗?

Thanks in advance. 提前致谢。

If you want to run the Test class, you should use 如果要运行Test类,则应该使用

 java -cp sample.jar com.app.Test

This way, you add the jar to the classpath and then run the specified main class. 这样,您将jar添加到类路径,然后运行指定的主类。

What java -jar does is that it executes a runnable jar file (which defines its own main class in the manifest file). java -jar所做的是它执行一个可运行的jar文件(它在清单文件中定义了自己的主类)。 Any parameters after that would not be used to specify the class, but end up in the String array passed to the main method. 之后的任何参数都不会用于指定类,而是以传递给main方法的String数组结束。

So if you have a properly constructed runnable jar file, it should just be 因此,如果你有一个正确构造的可运行jar文件,它应该是

java -jar sample.jar 

If you are using Maven, you may need to use maven's install command. 如果您使用的是Maven,则可能需要使用maven的install命令。 You can find the format here http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html 你可以在http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html找到这种格式。

Looks like you do need a manifest and a Main-Class attribute, if you don't have one. 看起来你需要一个清单和一个Main-Class属性,如果你没有。 If you are using java 7 a bug is filed here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7067922 如果您使用的是java 7,则会在此处提交一个错误http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7067922

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 执行不带“ java -jar”的jar文件(如命令) - Execute jar file without “java -jar” (like a command) 使用第三方jar在命令行中执行jar文件 - execute jar file in command line using third party jar 如何使用以下命令行在类 UNIX 中执行 jar 文件:java -jar mysolution.jar &lt; inputfile.txt - How can I execute jar file in UNIX-like using this command line: java -jar mysolution.jar < inputfile.txt 无法使用Java -jar执行Maven生成的jar文件 - Unable to execute maven generated jar file using java -jar 无法使用Java程序执行jar文件。 我需要将文件路径作为命令行参数传递给jar文件 - unable to execute the jar file using java program. i need to pass the file path as a command line argument to the jar file 从Java执行命令提示符的命令以创建jar文件 - Execute command prompt's command from java to create jar file 使用命令行将Java IO与Jar文件配合使用 - Java IO with a Jar file using command line 我已经使用maven构建了一个jar文件,但是无法在Linux中使用“ java”命令执行该文件 - I have built a jar file using maven, but cannot execute it using the “java” command in linux 如何从Java执行带有文件名的jar命令 - How to execute jar command with file name from java Java - JAR 文件不会从终端命令行执行 - Java - JAR file would not execute from the terminal command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM