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