java NetBeans 制作的 jar 文件不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11056871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NetBeans-made jar file won't work
提问by Bluefire
So I made this (very simple) program with a swing GUI with NetBeans, and I clicked build to make a jar file. When I run it by double clicking it, it tells me it could not find the main class, which, after checking, I am sure is definitely there. But, when I run it from Command Prompt, it works perfectly. Any easily-determinable reason for this strange behaviour (if you want the source code, I can post it here)?
所以我用 NetBeans 用一个 Swing GUI 制作了这个(非常简单的)程序,然后我点击 build 来制作一个 jar 文件。当我通过双击运行它时,它告诉我它找不到主类,经过检查,我确信它肯定存在。但是,当我从命令提示符运行它时,它运行良好。这种奇怪行为的任何易于确定的原因(如果你想要源代码,我可以在这里发布)?
回答by csd
The things that seem to be needed in NetBeans are:
NetBeans 中似乎需要的东西是:
- The project has to be the Main Project (by right clicking on it in the Projects list).
- You have to set the Main Class in the project properties. (Right click, Properties, Run, Main Class.)
- 该项目必须是主项目(通过在项目列表中右键单击它)。
- 您必须在项目属性中设置主类。(右键单击,属性,运行,主类。)
Then when you right click on your project and do a "Clean and Build", a jar will get built into the dist
subdirectory.
然后,当您右键单击您的项目并执行“清理并构建”时,将在dist
子目录中构建一个 jar 。
If that fails to fix the problem, here's a longer story...
如果这不能解决问题,这里有一个更长的故事......
When you double click a jar file to run it, the operating system acts as if you had typed this from the command line:
当你双击一个 jar 文件来运行它时,操作系统的行为就像你从命令行输入的一样:
java -jar filename.jar
(When you say it works for you from the command line, is this what you're typing?)
(当您从命令行说它对您有用时,这是您正在输入的内容吗?)
At that point, the Java executable looks for a file inside the jar named META-INF/MANIFEST.MF
. And then in the contents of that file, it looks for the value of a property, Main-Class
. And finally it looks for the class of that name in your jar and runs its static main(String[])
method.
此时,Java 可执行文件会在 jar 中查找名为META-INF/MANIFEST.MF
. 然后在该文件的内容中查找属性的值Main-Class
. 最后,它会在您的 jar 中查找该名称的类并运行其静态main(String[])
方法。
So if your jar is failing to run, you can do the following to debug what's going on:
因此,如果您的 jar 无法运行,您可以执行以下操作来调试正在发生的事情:
- Clean and rebuild your project in NetBeans.
- Double check that your class(es) are actually in the jar:
- Start a command prompt
cd
into thedist
subdirectory of your project.- Use a command like
jar tf filename.jar
to list what's in there.
- Double check that the
MANIFEST.MF
file is correct:- Again in a command prompt
cd
into thedist
directory.- Use a command like
jar xf filename.jar META-INF/MANIFEST.MF
to extract the manifest. - Look at the contents of that file (e.g.
type META-INF\MANIFEST.MF
) and make sureMain-Class
is set to the appropriate class.
- 在 NetBeans 中清理并重建您的项目。
- 仔细检查您的课程是否确实在 jar 中:
- 启动命令提示符
cd
进入dist
你的项目的子目录。- 使用命令 like
jar tf filename.jar
列出里面的内容。
- 仔细检查
MANIFEST.MF
文件是否正确:- 再次在命令提示符中
cd
进入dist
目录。- 使用像
jar xf filename.jar META-INF/MANIFEST.MF
提取清单的命令。 - 查看该文件的内容(例如
type META-INF\MANIFEST.MF
)并确保Main-Class
已设置为适当的类。
If all of the above check out, then double clicking the file shouldwork.
如果以上所有检查都通过,则双击该文件应该可以工作。
回答by BlackVegetable
Have you set the containing project as the "Main Project"?
您是否将包含项目设置为“主项目”?