简体   繁体   English

运行Eclipse导出的jar时“找不到主类”错误

[英]“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. 我有一个java项目,从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 当我尝试将其导出到"JAR file""Runnable JAR file" ,会创建.jar文件,但是当我双击它以尝试运行该程序时,它会给我一个错误

"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: 正如我所提到的,我尝试导出到两个JAR选项,我指定了main方法所在的正确类,当我查看.jar文件中的实际文件时,一切似乎都是有序的 - 清单看起来像:

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

and is in the META-INF folder. 并且位于META-INF文件夹中。 There is a folder with my package name, which contains all the .class files, including the class that contains the main method. 我的包名称有一个文件夹,其中包含所有.class文件,包括包含main方法的类。 A few image and text files that I use also appear in the jar file. 我使用的一些图像和文本文件也出现在jar文件中。

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). 实际的程序并不是太复杂 - 它是一个使用Swing的简单“蛇”游戏(加上代码在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. 如果我应该提供任何其他信息,请告诉我。

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? 您是否在Windows资源管理器窗口中双击该文件? 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. 请检查您的文件,缺少新行会造成麻烦。

在命令行上运行它:

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

Ok, so I finally got it to work. 好的,所以我终于开始工作了。 If I use the JRE 6 instead of 7 everything works great. 如果我使用JRE 6而不是7,一切都很好。 No idea why, but it works. 不知道为什么,但它的确有效。

Had you tried creating a .jar file manually instead of using Eclipse. 您是否尝试手动创建.jar文件而不是使用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 : 现在假设我的主类是BeginGameBeginGame里面的tictactoe ,所以我会在manifest.txt文件里写这个东西:

Main-Class: tictactoe.BeginGame

Do remember the space between colons : and package name ie tictactoe , and immediately after BeginGame press Enter and save the file. 请记住冒号之间的空格:和包名称即tictactoe ,并在BeginGame之后BeginGame按Enter并保存文件。

Now on your command prompt go to the location of bin folder, I am describing my side as follows : 现在在你的命令提示符下转到bin文件夹的位置,我正在描述我的一面如下:

C:\Mine\Eclipse\TicTacToe\bin>jar -cfm ..\tictactoe.jar ..\manifest.txt tictactoe icons
  • Here the first argument ie ..\\tictactoe.jar is used to tell that create tictactoe.jar one level up, ie inside TicTacToe Folder . 这里第一个参数ie ..\\tictactoe.jar用于告诉创建tictactoe.jar一级,即在TicTacToe Folder
  • Second argument ..\\manifest.txt means that the manifest.txt file is located one level up, ie inside TicTacToe Folder . 第二个参数..\\manifest.txt意味着manifest.txt文件位于一个级别,即在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. 第三和第四个参数tictactoeicons表示将这两个文件夹添加到.jar文件中,因为它们放在bin文件夹中,因此它们按原样使用。 Now Press Enter. 现在按Enter键。

Now try to run your .jar file so created inside the Project Folder (TicTacToe Folder, in my case). 现在尝试运行在Project Folder (TicTacToe文件夹,在我的例子中)中创建的.jar文件。

Hopefully this will work. 希望这会奏效。

Have you renamed your project/main class (eg through refactoring) ? 您是否重命名了项目/主类(例如通过重构)? If yes your Launch Configuration might be set up incorrectly (eg 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. 即使项目名称出现在“export runnable jar”对话框中,仔细检查也可能会显示一个不匹配的主类名称。

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 . 转到您的项目的“属性- >运行/调试设置”,并确保您的启动配置(用于导出运行的JAR相同)设置为项目的正确名字你的主类设置为name.space.of.your.project/YouMainClass

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. 错误消息是“无法找到主类”,但我可以运行从其他Windows机器中的Eclipse导出的可执行jar,没有任何问题。

The solution was to install both x64 and x86 version of the same version of JRE. 解决方案是安装相同版本的JRE的x64和x86版本。 The path environment variable was pointed to the x64 version. 路径环境变量指向x64版本。 No idea why, but it worked for me. 不知道为什么,但它对我有用。

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. 现在,当你再次跑步时,事情会好起来的。

For netbeans user that having this problem is as simply: 对于有这个问题的netbeans用户来说简单:

1.Go to your Project and Right Click and Select Properties 1.转到您的项目并右键单击并选择属性

2.Click Run and also click browser. 2.单击“运行”,然后单击“浏览器

3.Select your frames you want to first appear. 3.选择要首次显示的帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM