java -jar 运行jar包,无法使用java -jar命令执行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 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)

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

Thanks in advance.

解决方案

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

java -cp sample.jar com.app.Test

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

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.

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

java -jar sample.jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值