Java 在 NetBeans 中生成可执行的 jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/602537/
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
Producing executable jar in NetBeans
提问by Dani
I'm using NetBeans 6.5 and for some reason it won't produce executable jar "out of the box".
我使用的是 NetBeans 6.5,出于某种原因,它不会“开箱即用”生成可执行的 jar。
I set my project to be the main project, defined main class in the project properties "run" menu and it works flawlessly when I press F6 to run it.
我将我的项目设置为主项目,在项目属性“运行”菜单中定义了主类,当我按 F6 运行它时,它可以完美运行。
I looked at the manifest file and it indeed didn't define the main class there, and also omitted the library dependencies.
我查看了清单文件,它确实没有在那里定义主类,并且还省略了库依赖项。
Am I missing something? Is there a way (other than manually altering the manifest file) to produce executable jar files?
我错过了什么吗?有没有办法(除了手动更改清单文件)来生成可执行的 jar 文件?
EDIT: yes, I tried clean and rebuild and it produced the jar in the dist folder, still with the same manifest.mf
编辑:是的,我尝试清理和重建,它在 dist 文件夹中生成了 jar,仍然具有相同的 manifest.mf
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 10.0-b23 (Sun Microsystems Inc.)
回答by John Ellinwood
Did you clean the project and rebuild? It sound like you're doing it right. Heres some references just in case:
你清理了项目并重建了吗?听起来你做对了。以下是一些参考资料,以防万一:
http://www.velocityreviews.com/forums/t141385-how-to-create-executable-jar-in-netbeans-ide.htmlhttp://dr.berkeley.edu/REM/wiki/index.php/Making_a_Java_executable_jar_in_Netbeans
http://www.velocityreviews.com/forums/t141385-how-to-create-executable-jar-in-netbeans-ide.html http://dr.berkeley.edu/REM/wiki/index.php/Making_a_Java_executable_jar_in_Netbeans
回答by Rogier
Strange indeed, it should do it out of the box including the classpath.
确实很奇怪,它应该开箱即用,包括类路径。
Did you upgrade from a previous version? When upgrading, NB will upgrade the project files but sometimes this migration is not done well and this kind of scenario's pop up. Just close the project, rename the nbproject dir to nbproject_old and do new project -> Java project with existing sources. Set the main class again and add dependencies and try again.
你是从以前的版本升级的吗?升级的时候NB会升级工程文件,但是有时候这个迁移做的不好,会弹出这种场景。只需关闭项目,将 nbproject 目录重命名为 nbproject_old 并使用现有源执行新项目 -> Java 项目。再次设置主类并添加依赖项并重试。
回答by James Kingsbery
Another approach entirely is to develop, run unit tests, etc. in NetBeans but then use a maven or ant script to do your "real" build. It seems like these kinds of build tools give you more control over how things are built, and it also lets you automate builds.
完全另一种方法是在 NetBeans 中开发、运行单元测试等,然后使用 maven 或 ant 脚本进行“真正的”构建。似乎这些构建工具可以让您更好地控制事物的构建方式,并且还可以让您自动构建。
For maven, you can add entries to the manifest easily using the maven jar plugin:
对于 maven,您可以使用 maven jar 插件轻松地向清单添加条目:
回答by l_39217_l
Goto the Files view. Create a manifest.mf in the root directory.
转到文件视图。在根目录中创建一个 manifest.mf。
/project_folder
build.xml
manifest.mf(edit here)
/dist
/src
/test
/build
回答by marktucks
If you clean and build your project it should create the jar in the "dist" directory.
如果您清理并构建您的项目,它应该在“dist”目录中创建 jar。
It should create the manifest.mf at the top level of your project directory.
它应该在项目目录的顶层创建 manifest.mf。
回答by marktucks
I actually came across this page after running into the same issue, I edited the manifest.mf file but the changes weren't showing up in the jar MANIFEST.MF file. I found the issue that I was having and thought I would pass along the information just in case it's relevant.
在遇到同样的问题后,我实际上遇到了这个页面,我编辑了 manifest.mf 文件,但更改没有显示在 jar MANIFEST.MF 文件中。我发现了我遇到的问题,并认为我会传递信息,以防万一。
What I was doing wrong was I didn't have the project I was working on set as the main project, so while I was editing the the correct manifest, I was compiling the wrong project.
我做错的是我没有将我正在处理的项目设置为主项目,所以当我编辑正确的清单时,我编译了错误的项目。
So I suppose the short of the story did you check and see if you have the correct project selected for the manifest.mf file you are editing?
所以我想您是否检查并查看您是否为正在编辑的 manifest.mf 文件选择了正确的项目?
回答by marktucks
Maybe you created Java Project with Existing Sourcesinstead of Java Application?
也许您使用现有源创建了Java 项目而不是Java 应用程序?
I had similar problem and creating new Java Application and then manually copying src files solved the problem - everything worked "out of the box".
我有类似的问题,创建新的 Java 应用程序,然后手动复制 src 文件解决了这个问题 - 一切都“开箱即用”。
回答by ivan_ivanovich_ivanoff
I had the experience, that the build process is different, depending on the project type.
我的经验是,构建过程因项目类型而异。
I suppose, you've created "Java Class Library" project.
我想,您已经创建了“Java 类库”项目。
So just create a "Java Application" project, then merge all classes to it.
所以只需创建一个“Java 应用程序”项目,然后将所有类合并到它。
Then, Netbeans will not override, but enhancethe "manifest.mf"
into the JAR.
然后,NetBeans将不会覆盖,而是增强了"manifest.mf"
成JAR。
Of course, it's a stupid Netbeans bug. Of course it should be possible to add main classes afterwards.
当然,这是一个愚蠢的 Netbeans 错误。当然,之后应该可以添加主类。
EDIT:Please see my other answer. It's easier.
编辑:请参阅我的另一个答案。这更容易。
回答by ivan_ivanovich_ivanoff
A better solution:
更好的解决方案:
Edit nbproject/project.properties and add the entry:
编辑 nbproject/project.properties 并添加条目:
manifest.file=manifest.mf
manifest.mf
should be in project's root and contain just:
manifest.mf
应该在项目的根目录中并且只包含:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Works. Tested.
作品。测试。
回答by kv.
Try this:
尝试这个:
Right click your project on the "Projects" panel, select "Properties"
Click on "Run" in the new window.
Edit the "Main Class:" field (click on Browse).
在“项目”面板上右键单击您的项目,选择“属性”
在新窗口中单击“运行”。
编辑“主类:”字段(单击浏览)。
This way you will select the main class which is the entry point to your application and the Manifest will be created correctly.
通过这种方式,您将选择作为应用程序入口点的主类,并且将正确创建清单。