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

9

32

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.

Anjanette answered 8/3, 2012 at 4:32 Comment(6)
What is the fully qualified name of the main class? Is it package.MainClassName or something else?Scavenger
Manifesto is a convenient way to examine your manifest in place.Leavitt
@Leavitt what exactly does Manifesto do that just looking at the manifest does not?Anjanette
@scae: Just convenience; it shows the manifest as it actually appears in the JAR, without having to extract the contents.Leavitt
possible duplicate of "Could not find the main class" when double-clicking .jar fileDeerstalker
where do you view the manifest?Distill
A
7

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.

Anjanette answered 8/3, 2012 at 6:41 Comment(4)
Ahha, there is a version clash, your eclipse is using a higher version, then the one on the classpath. It happens most of the time.Puzzlement
any way to fix it besides just using the older version in eclipse?Anjanette
Instead select same version on both your Eclipse and Path Variable in Environment Variables.Puzzlement
In eclipse, go to the run configurations of your project and select the "Main class" (browse and select). After this generate the jar file. This should resolve any issues.Pre
P
27

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.

Plural answered 8/3, 2012 at 5:42 Comment(1)
sorry, I forgot to mention it above but the manifest is created by Eclipse and does include a blank line at the end (sometimes it includes 2).Anjanette
S
8

Run it like this on the command line:

java -jar /path/to/your/jar/jarFile.jar
Selry answered 30/4, 2014 at 12:10 Comment(2)
Can someone please explain what is wrong with this answer? I don't get it. This is exactly what I use to run a jar file when I get the "cannot find main class error"Selry
This worked for me, forgot the "-" in front of "jar"Rector
A
7

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.

Anjanette answered 8/3, 2012 at 6:41 Comment(4)
Ahha, there is a version clash, your eclipse is using a higher version, then the one on the classpath. It happens most of the time.Puzzlement
any way to fix it besides just using the older version in eclipse?Anjanette
Instead select same version on both your Eclipse and Path Variable in Environment Variables.Puzzlement
In eclipse, go to the run configurations of your project and select the "Main class" (browse and select). After this generate the jar file. This should resolve any issues.Pre
P
5

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.

Puzzlement answered 8/3, 2012 at 6:31 Comment(0)
G
1

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.

Guzman answered 14/8, 2012 at 13:14 Comment(0)
T
1

I ran into the same issues the other day and it took me days to make it work. The error message was "Could not find the main class", but I can run the executable jar exported from Eclipse in other Windows machines without any problem.

The solution was to install both x64 and x86 version of the same version of JRE. The path environment variable was pointed to the x64 version. No idea why, but it worked for me.

Tod answered 2/2, 2018 at 7:18 Comment(0)
B
0

Right click on the project. Go to properties. Click on Run/Debug Settings. Now delete the run config of your main class that you are trying to run. Now, when you hit run again, things would work just fine.

Bridle answered 12/10, 2015 at 6:33 Comment(0)
T
0

For netbeans user that having this problem is as simply:

1.Go to your Project and Right Click and Select Properties

2.Click Run and also click browser.

3.Select your frames you want to first appear.

Towering answered 31/8, 2017 at 7:18 Comment(0)
J
0

you are just missing static keyword. that's all.

Jolt answered 22/4, 2022 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.