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

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

问题描述

我创建了一个 jar 文件使用 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

Test 是具有主要方法的类.但我收到此异常:

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天全站免登陆