2

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.

4
  • Did you include a MANIFEST file in your jar that specifies the main class to run? See: download.oracle.com/javase/6/docs/technotes/guides/jar/… Commented Nov 19, 2011 at 7:39
  • Maybe the manifest file is broken? Can you open the jar file and check that the main class is properly defined in META-INF/Manifest ? Commented Nov 19, 2011 at 7:39
  • can you run it outside of the jar and it works? Commented Nov 19, 2011 at 7:39
  • Did you create a manifest file: docs.oracle.com/javase/tutorial/deployment/jar/… Commented Jul 26, 2013 at 20:39

3 Answers 3

7

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 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Thilo, how to specify entry class in manifest file at the time of building the jar?
Or, using Maven (which can also bundle in all dependencies, which is something you want): stackoverflow.com/questions/2022032/…
2

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

Comments

0

Looks like you do need a manifest and a Main-Class attribute, if you don't have one. If you are using java 7 a bug is filed here https://bugs.java.com/bugdatabase/view_bug?bug_id=7067922

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.