可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
Run it like this on the command line:
java -jar /path/to/your/jar/jarFile.jar
回答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
.
回答6:
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.
回答7:
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.
回答8:
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.