1

I am trying to do something really simple but I don't know how to do it. I have a third party jar file which has the class definition. I have a class with the main method. The main class uses a class in the jar file. I compiled with this command option. There was no error complaint.

javac -classpath party.jar mymain.java

When I tried to run the program, I got the following error:

java -jar party.jar myMain

Exception in thread "main" java.lang.NullPointerException at sun.launcher.LauncherHelper.getMainClassFromJar(LauncherHelper.java:399)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:463)

I think I need to add the Main-Class option in the jar file. But I am not allow to change the party.jar file because it belongs to a third party. What do I need to do to run my program ? Do I need to create my own jar file ? If yes what do I need to include in there ?

This is the exact commands and output

myjar]$ javac -cp party.jar mymain.java

myjar]$ java -cp party.jar mymain

Error: Could not find or load main class mymain

tadpole
  • 1,209
  • 7
  • 19
  • 28

2 Answers2

3

You should try java -cp party.jar myMain instead. Using -jar you tell the JVM to look for your class in the given jar file. But party.jar does not include your myMain class, if i understood correctly. So you just have so tell Java that you wish to use classes from party.jar using the -cp option (like you did for compiling), but take the myMain class from the directory you are in.

pushy
  • 9,535
  • 5
  • 26
  • 45
  • 1) java -cp party.jar myMain. I got this error "Error: Could not find or load main class mymain" – tadpole Mar 19 '12 at 13:21
  • Either you did not enter that command, or did not get that output. The case is different for the class name. – Andrew Thompson Mar 19 '12 at 13:24
  • You can also try to run your program using 'java mymain' first. If it throws a ClassNotFoundException with the name of the class you imported from party.jar, you are fine and just have to add -cp party.jar to make it work. If you get an error with mymain you should check that mymain.class exists in the directory you are in, and no package declaration was used in mymain.java – pushy Mar 19 '12 at 13:28
  • I tried to run java mymain. I got the same error."Error: Could not find or load main class mymain". – tadpole Mar 19 '12 at 13:33
  • If you have problems running your java class you can refer to this question: http://stackoverflow.com/questions/6314648/how-to-run-this-java-class-in-command-line-in-windows – pushy Mar 19 '12 at 13:42
0

You should try java -cp party.jar; myMain instead. If you using multiple jar files put semicolon after each one. Remember to put space after last semicolon just before the main class name(myMain here). Hope, it works.