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

115,885

Solution 1

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.

Solution 2

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


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

Solution 3

Run it like this on the command line:

java -jar /path/to/your/jar/jarFile.jar

Solution 4

Had you tried creating a .jar file manually instead of using Eclipse. Try the following steps, hopefully that might help :

Considering that your directory structure looks like this :

 TicTacToe(Your Project Name I mean)
 |              |                  |
src            bin             manifest.txt
             |    |
           icons  tictactoe

Now suppose that my main class is BeginGame inside package tictactoe, so I will write inside my manifest.txt file this thing :

Main-Class: tictactoe.BeginGame

Do remember the space between colons : and package name i.e. tictactoe, and immediately after BeginGame press Enter and save the file.

Now on your command prompt go to the location of bin folder, I am describing my side as follows :

C:\Mine\Eclipse\TicTacToe\bin>jar -cfm ..\tictactoe.jar ..\manifest.txt tictactoe icons
  • Here the first argument i.e. ..\tictactoe.jar is used to tell that create tictactoe.jar one level up, i.e. inside TicTacToe Folder.
  • Second argument ..\manifest.txt means that the manifest.txt file is located one level up, i.e. inside TicTacToe Folder.
  • Third and Fourth arguments tictactoe and icons means, that add both these folders to the .jar file, since they are placed inside bin Folder so they are used as it is. Now Press Enter.

Now try to run your .jar file so created inside the Project Folder (TicTacToe Folder, in my case).

Hopefully this will work.

Solution 5

Have you renamed your project/main class (e.g. through refactoring) ? If yes your Launch Configuration might be set up incorrectly (e.g. refering to the old main class or configuration). Even though the project name appears in the 'export runnable jar' dialog, a closer inspection might reveal an unmatched main class name.

Go to 'Properties->Run/Debug Settings' of your project and make sure your Launch Configuration (the same used to export runnable jar) is set to the right name of project AND your main class is set to name.space.of.your.project/YouMainClass.

Share:
115,885
scaevity
Author by

scaevity

Updated on February 02, 2020

Comments

  • scaevity
    scaevity about 4 years

    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.