Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not find the main class" error when running jar exported by Eclipse

I have a java project that works perfectly fine when running it from within Eclipse. When I try to export it to either a "JAR file" or "Runnable JAR file" the .jar file is created, but when I double click on it to try to run the program it gives me an error that says

"Could not find the main class: package.MainClassName. Program will exit."

As I mentioned, I tried exporting to both JAR options, I specified the correct class that the main method is in, and when I look through the actual files in the .jar file everything seems to be in order -- the manifest looks something like:

Manifest-Version: 1.0
Main-Class: package.MainClassName
(blank line)

and is in the META-INF folder. There is a folder with my package name, which contains all the .class files, including the class that contains the main method. A few image and text files that I use also appear in the jar file.

The actual program isn't anything too complicated -- it's a simple "snake" game using Swing (plus the code all works when run from inside Eclipse).

Any ideas what is causing this error and how I can fix it? Let me know if there's any other information I should provide.

like image 1000
scaevity Avatar asked Mar 08 '12 04:03

scaevity


3 Answers

Verify that you can start your application like that:

java -cp myjarfile.jar snake.Controller

I just read when I double click on it - this sounds like a configuration issue with your operating system. You're double-clicking the file on a windows explorer window? Try to run it from a console/terminal with the command

java -jar myjarfile.jar

Further Reading

  • Running JAR file on Windows

The manifest has to end with a new line. Please check your file, a missing new line will cause trouble.

like image 131
Andreas Dolk Avatar answered Oct 02 '22 16:10

Andreas Dolk


Ok, so I finally got it to work. If I use the JRE 6 instead of 7 everything works great. No idea why, but it works.

like image 40
scaevity Avatar answered Oct 02 '22 17:10

scaevity


Run it like this on the command line:

java -jar /path/to/your/jar/jarFile.jar
like image 26
MobileMon Avatar answered Oct 02 '22 17:10

MobileMon