简体   繁体   English

NetBeans制作的jar文件无效

[英]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中似乎需要的东西是:

  • 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. 然后,当您右键单击项目并执行“清理并构建”时,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无法运行,您可以执行以下操作来调试正在进行的操作:

  • Clean and rebuild your project in NetBeans. 在NetBeans中清理并重建项目。
  • Double check that your class(es) are actually in the jar: 仔细检查你的类实际上是在jar中:
    • Start a command prompt 启动命令提示符
    • cd into the dist subdirectory of your project. cd进入项目的dist子目录。
    • Use a command like jar tf filename.jar to list what's in there. 使用像jar tf filename.jar这样的命令来列出那里的内容。
  • Double check that the MANIFEST.MF file is correct: 仔细检查MANIFEST.MF文件是否正确:
    • Again in a command prompt 再次在命令提示符下
    • cd into the dist directory. cd进入dist目录。
    • Use a command like jar xf filename.jar META-INF/MANIFEST.MF to extract the manifest. 使用jar xf filename.jar META-INF/MANIFEST.MF类的命令来提取清单。
    • Look at the contents of that file (eg 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.

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