[英]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.