3

I have been testing a java swing program that I have been making. On my one computer, the one I originally made it on, it works fine. I have tested the same program on 3 different computers and it runs when I launch it out of the ide, but when I double click the jar I get a popup error window titled 'Java Virtual machien Launcher'. The error is "Could not find the main class: xxxxxxx. Program will exit."

I cannot figure out for the life of me what is going on. It was working before.

NJGUY
  • 2,045
  • 3
  • 23
  • 43
  • Jar files are just zip files. Rename it to `.zip` and check if a) the .class file is in there (and in the proper package folder) and b) you have a proper manifest file with a `main-class` entry and c) your main class has package – ZeissS Jun 15 '12 at 13:46
  • Read a tutorial talking about META_INF and Manifest file, it'll definitely help you. – Michael Laffargue Jun 15 '12 at 13:46
  • 3
    possible duplicate of ["Could not find the main class: XX. Program will exit."](http://stackoverflow.com/questions/1417328/could-not-find-the-main-class-xx-program-will-exit) – Robin Jun 15 '12 at 13:46
  • Have a look at this example [How to create .jar files manually](http://stackoverflow.com/questions/9612918/could-not-find-the-main-class-error-when-running-jar-exported-by-eclipse/9613766#9613766) – nIcE cOw Jun 15 '12 at 14:23

1 Answers1

4

You need to include a Manifest file within your jar. In this, you specify which class is to be used as the entry point when the jar gets launched.

Create a file called Manifest.txt, and add:

 Main-Class: yourMainClass.class

Then, to create the jar :

jar cfm JarName.jar Manifest.txt yourMainClass/*.class

To run the from the command line, use : java -jar JarName.jar

YYZ
  • 749
  • 4
  • 8
  • 18
  • 1
    You just failed to mention, then after adding `Main-Class: yourMainClass` , here .class is really not required, Moreover after writing this, you forgot to mention that one has to press `ENTER` key once and then save the file. +1 for the creation part though – nIcE cOw Jun 15 '12 at 14:25
  • 1
    @nIcEcOw you're completely right about hitting enter. If the manifest file doesn't end with a carriage return, it won't be parsed properly. .class isn't explicitly needed, I just felt it was worthwhile to include it to make the answer crystal clear. – YYZ Jun 15 '12 at 15:25