无法使用java -jar命令执行jar文件 [英] Failing to execute the jar file using java -jar command

查看:1587
本文介绍了无法使用java -jar命令执行jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个jar文件usign maven2 build。我正在尝试使用以下命令运行该jar文件:

I have created a jar file usign maven2 build. I am trying to run that jar file using the command:

java -jar sample.jar com.app.Test

测试是具有main方法的类。但是我得到了这个例外:

Test being the class which is having the main method. 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)

任何人都可以帮我解决这个异常并运行jar文件吗?

Can any one help me to solve this exception and run the jar file?

提前致谢。

推荐答案

如果你想运行Test类,你应该使用

If you want to run the Test class, you should use

 java -cp sample.jar com.app.Test

这样,你将jar添加到类路径然后运行指定的主类。

This way, you add the jar to the classpath and then run the specified main class.

什么是 java -jar 做的是它执行一个可运行的jar文件(定义清单文件中自己的主类)。之后的任何参数都不会用于指定类,但最终会传递给传递给main方法的String数组。

What java -jar does is that it executes a runnable jar file (which defines its own main class in the manifest file). Any parameters after that would not be used to specify the class, but end up in the String array passed to the main method.

所以如果你有一个正确构造的可运行jar文件,它应该只是

So if you have a properly constructed runnable jar file, it should just be

java -jar sample.jar 

这篇关于无法使用java -jar命令执行jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆