[英]NetBeans-made jar file won't work
So I made this (very simple) program with a swing GUI with NetBeans, and I clicked build to make a jar file. 所以我使用带有NetBeans的swing GUI制作了这个(非常简单的)程序,然后我点击了build来制作一个jar文件。 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)?
这种奇怪行为的任何容易确定的原因(如果你想要源代码,我可以在这里发布)?
The things that seem to be needed in NetBeans are: NetBeans中似乎需要的东西是:
Then when you right click on your project and do a "Clean and Build", a jar will get built into the dist
subdirectory. 然后,当您右键单击项目并执行“清理并构建”时,jar将内置到
dist
子目录中。
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
. 此时,Java可执行文件在名为
META-INF/MANIFEST.MF
的jar中查找文件。 And then in the contents of that file, it looks for the value of a property, Main-Class
. 然后在该文件的内容,它会寻找一个属性,值
Main-Class
。 And finally it looks for the class of that name in your jar and runs its static main(String[])
method. 最后,它在jar中查找该名称的类,并运行其静态
main(String[])
方法。
So if your jar is failing to run, you can do the following to debug what's going on: 因此,如果您的jar无法运行,您可以执行以下操作来调试正在进行的操作:
cd
into the dist
subdirectory of your project. cd
进入项目的dist
子目录。 jar tf filename.jar
to list what's in there. jar tf filename.jar
这样的命令来列出那里的内容。 MANIFEST.MF
file is correct: MANIFEST.MF
文件是否正确:
cd
into the dist
directory. cd
进入dist
目录。 jar xf filename.jar META-INF/MANIFEST.MF
to extract the manifest. jar xf filename.jar META-INF/MANIFEST.MF
类的命令来提取清单。 type META-INF\\MANIFEST.MF
) and make sure Main-Class
is set to the appropriate class. type META-INF\\MANIFEST.MF
)并确保将Main-Class
设置为适当的类。 If all of the above check out, then double clicking the file should work. 如果以上所有内容都签出,则双击该文件应该可以正常工作。
您是否将包含项目设置为“主项目”?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.