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.

talnicolas
  • 13,885
  • 7
  • 36
  • 56
user972590
  • 251
  • 1
  • 5
  • 13
  • Did you include a MANIFEST file in your jar that specifies the main class to run? See: http://download.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#JAR%20Manifest –  Nov 19 '11 at 07: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 ? – Thilo Nov 19 '11 at 07:39
  • can you run it outside of the jar and it works? – pajton Nov 19 '11 at 07:39
  • Did you create a manifest file: http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html – amb110395 Jul 26 '13 at 20:39

3 Answers3

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 
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Thanks Thilo, how to specify entry class in manifest file at the time of building the jar? – user972590 Nov 19 '11 at 07:48
  • `Main-Class: com.app.Test` http://download.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#JAR%20Manifest – Thilo Nov 19 '11 at 07:49
  • Or, using Maven (which can also bundle in all dependencies, which is something you want): http://stackoverflow.com/questions/2022032/building-a-runnable-jar-with-maven-2 – Thilo Nov 19 '11 at 07:50
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

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 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7067922

reevesy
  • 3,452
  • 1
  • 26
  • 23