在 NetBeans 中生成可执行 jar

发布于 2024-07-14 13:20:16 字数 432 浏览 4 评论 0原文

我正在使用 NetBeans 6.5,由于某种原因,它不会生成“开箱即用”的可执行 jar。

我将我的项目设置为主项目,在项目属性“运行”菜单中定义主类,当我按 F6 运行它时,它可以完美地工作。

我查看了清单文件,它确实没有定义主类,并且还省略了库依赖项。



我错过了什么吗? 有没有办法(除了手动更改清单文件之外)生成可执行的 jar 文件?

编辑:是的,我尝试清理并重建,它在 dist 文件夹中生成了 jar,仍然具有相同的 manifest.mf

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 10.0-b23 (Sun Microsystems Inc.)

I'm using NetBeans 6.5 and for some reason it won't produce executable jar "out of the box".

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.

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?

EDIT: yes, I tried clean and rebuild and it produced the jar in the dist folder, still with the same manifest.mf

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 10.0-b23 (Sun Microsystems Inc.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

你是暖光i 2024-07-21 13:20:17

要使用 NetBeans IDE 8.0.2 手动强制创建...

  1. 在项目导航器中,展开您的项目> 右键单击 Project Files / pom.xml
  2. 单击 Run Maven /Goals
  3. 在目标字段中键入“package”
  4. 单击确定

然后输出窗口将显示(在 Mac 上)...

Building jar: /Users/<username>/NetBeansProjects/<project>/target/<project>-1.0-SNAPSHOT.jar

To manually force package creation with NetBeans IDE 8.0.2 ...

  1. In Project navigator, expand your <project> and right click Project Files / pom.xml
  2. Click Run Maven / Goals
  3. Type "package" in Goals field
  4. Click OK

The output window will then show (on Mac) ...

Building jar: /Users/<username>/NetBeansProjects/<project>/target/<project>-1.0-SNAPSHOT.jar
终止放荡 2024-07-21 13:20:17

好的,使用 neonbean,您可以执行以下步骤:
1. 右键单击​​名称项目选择项目设置配置并选择自定义
2. 选择运行并注意主类
3. 如果要运行,请选择浏览并设置类。 单击“确定”后。
4. 完成。

Ok with neatbean you can do following steps below:
1. Right click name project choose item Set Configuration and Choose Customize
2. Choose Run and notice Main Class
3. Choose Browse and set class if you want to run. After you click ok.
4. Finish.

你与清晨阳光 2024-07-21 13:20:16

我刚刚在 NetBeans 7.2.1 中的 Maven Java 应用程序项目中遇到了同样的问题。 修改 pom.xml 文件以包含 maven 程序集插件,对 myrho 的答案进行了一些调整(需要引用预定义描述符“jar-with-dependencies”):

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>your.app.MainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

另一种方法是在 NetBeans 中构建一个不使用 Maven 的 Java 应用程序项目。 选择文件-> 项目属性-> 构建-> 打包并选中“复制依赖库”复选框。

I just had the same problem in NetBeans 7.2.1 with a Maven Java Application project. Modify the pom.xml file to include the maven assembly plugin with one tweak to myrho's answer (needs to reference the predefined descriptor "jar-with-dependencies"):

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>your.app.MainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

An alternate method is to build a Java Application project in NetBeans that doesn't use Maven. Select File -> Project Properties -> Build -> Packaging and check the "Copy Dependent Libraries" checkbox.

陪你搞怪i 2024-07-21 13:20:16

试试这个:

  1. 在“项目”面板上右键单击您的项目,选择“属性”

  2. 单击“运行”新窗口。

  3. 编辑“主类:”字段(单击“浏览”)。

这样,您将选择作为应用程序入口点的主类,并且将正确创建清单。

Try this:

  1. Right click your project on the "Projects" panel, select "Properties"

  2. Click on "Run" in the new window.

  3. 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.

幸福%小乖 2024-07-21 13:20:16

如果您使用 maven assembly 插件并希望构建具有依赖项的可执行 jar,则需要将此部分添加到 pom.xml 中 maven- assembly-plugin 部分的配置中:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <!-- ... -->
    <archive>
      <manifest>
        <mainClass>your.app.SampleClass</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

Source: Maven 组件插件用法

If you are using the maven assembly plugin and want to build an executable jar with dependencies, you need to add this portion to the configuration of the maven-assembly-plugin section in your pom.xml:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <!-- ... -->
    <archive>
      <manifest>
        <mainClass>your.app.SampleClass</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

Source: Maven Assembly Plugin Usage

酒解孤独 2024-07-21 13:20:16

非常简单,.jar 和 .jad 文件位于 dist 文件夹中,

我也在寻找答案并从 http://forums.netbeans.org/ptopic3465.html

已解决:)

Very simple, The .jar and .jad files are there in dist folder

I was also searching for answer and got this one from http://forums.netbeans.org/ptopic3465.html

Solved :)

明月夜 2024-07-21 13:20:16

如果您清理并构建项目,它应该在“dist”目录中创建 jar。

它应该在项目目录的顶层创建manifest.mf。

If you clean and build your project it should create the jar in the "dist" directory.

It should create the manifest.mf at the top level of your project directory.

吾性傲以野 2024-07-21 13:20:16

事实上,我在遇到同样的问题后遇到了这个页面,我编辑了manifest.mf文件,但所做的更改没有显示在jar MANIFEST.MF文件中。 我发现了我遇到的问题,并认为我会传递这些信息,以防万一它是相关的。

我做错的是我没有将我正在处理的项目设置为主项目,所以当我编辑正确的清单时,我正在编译错误的项目。

所以我想您是否检查过您是否为正在编辑的manifest.mf 文件选择了正确的项目?

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.

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?

平安喜乐 2024-07-21 13:20:16

也许您使用现有源创建了 Java 项目而不是 Java 应用程序?

我遇到了类似的问题,创建新的 Java 应用程序,然后手动复制 src 文件解决了问题 - 一切都“开箱即用”。

Maybe you created Java Project with Existing Sources instead of Java Application?

I had similar problem and creating new Java Application and then manually copying src files solved the problem - everything worked "out of the box".

紧拥背影 2024-07-21 13:20:16

确实很奇怪,它应该开箱即用,包括类路径。

您是否从以前的版本升级? 升级的时候,NB会升级工程文件,但是有时候这个迁移做得不好,就会出现这种情况。 只需关闭项目,将 nbproject 目录重命名为 nbproject_old 并执行新项目 -> 具有现有源的 Java 项目。 重新设置主类并添加依赖项,然后重试。

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.

相思碎 2024-07-21 13:20:16

另一种方法完全是在 NetBeans 中开发、运行单元测试等,然后使用 Maven 或 ant 脚本来执行“真正的”构建。 似乎这些类型的构建工具可以让您更好地控制事物的构建方式,并且还可以让您自动化构建。

对于 maven,您可以使用 maven jar 插件轻松地将条目添加到清单中:

http: //maven.apache.org/plugins/maven-jar-plugin/

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.

For maven, you can add entries to the manifest easily using the maven jar plugin:

http://maven.apache.org/plugins/maven-jar-plugin/

九厘米的零° 2024-07-21 13:20:16

我有经验,构建过程是不同的,
取决于项目类型。

我想,您已经创建了“Java 类库”项目。

因此,只需创建一个“Java 应用程序”项目,然后将所有类合并到其中即可。

然后,Netbeans 将不会覆盖“manifest.mf” 到 JAR 中。

当然,这是一个愚蠢的 Netbeans 错误。 当然应该可以添加 main
之后上课。

编辑:请参阅我的其他答案。 更容易了。

I had the experience, that the build process is different,
depending on the project type.

I suppose, you've created "Java Class Library" project.

So just create a "Java Application" project, then merge all classes to it.

Then, Netbeans will not override, but enhance the "manifest.mf" into the JAR.

Of course, it's a stupid Netbeans bug. Of course it should be possible to add main
classes afterwards.

EDIT: Please see my other answer. It's easier.

吐个泡泡 2024-07-21 13:20:16

更好的解决方案:

编辑 nbproject/project.properties 并添加条目

manifest.file=manifest.mf

manifest.mf 应该位于项目的根目录中并仅包含:

Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

Works。 已测试。

A better solution:

Edit nbproject/project.properties and add the entry:

manifest.file=manifest.mf

manifest.mf should be in project's root and contain just:

Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

Works. Tested.

沫离伤花 2024-07-21 13:20:16

有趣的信息可能在这里:

http://wiki.netbeans.org/FaqNoMainClass

Interesting information might be here:

http://wiki.netbeans.org/FaqNoMainClass

慈悲佛祖 2024-07-21 13:20:16

转到文件视图。
在根目录中创建一个manifest.mf。

/project_folder
    build.xml
    manifest.mf(edit here)
    /dist
    /src
    /test
    /build

Goto the Files view.
Create a manifest.mf in the root directory.

/project_folder
    build.xml
    manifest.mf(edit here)
    /dist
    /src
    /test
    /build
通知家属抬走 2024-07-21 13:20:16

在 7.3 中,只需启用 Properties/Build/Package/Copy Dependent Libraries,主类(取决于所选目标)将在构建时添加到清单中。

In 7.3 just enable Properties/Build/Package/Copy Dependent Libraries and main class (depending on selected target) will be added to manifest when building.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文