Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing to execute the jar file using java -jar command

Tags:

java

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.

like image 609
user972590 Avatar asked Feb 11 '26 08:02

user972590


2 Answers

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 
like image 182
Thilo Avatar answered Feb 12 '26 22:02

Thilo


If you are using Maven, you may need to use maven's install command. You can find the format here http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

like image 22
user1070254 Avatar answered Feb 13 '26 00:02

user1070254



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!