如何在 NetBeans 项目生成的 jar 中的清单文件中设置 Main 类 [英] How to setup Main class in manifest file in jar produced by NetBeans project

查看:23
本文介绍了如何在 NetBeans 项目生成的 jar 中的清单文件中设置 Main 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题.我的 NetBeans IDE 6.8 中有一个 Java 项目.当我编译它并生成一个包含所有可能的 .jar 文件时,META-INF 是不正确的.它不包含要执行的类 - 使用 main() 方法.

I have the following problem. I have a Java project in my NetBeans IDE 6.8. When I compile it and it produces a .jar file containing everything possible, the META-INF is not right. It doesn't contain the class to be executed - with main() method.

当我单击 IDE 中的运行"按钮时,一切正常.项目的设置也设置正确——指向我项目中的一个类.

When I click the Run button inside the IDE, everything works. The settings of the project are also set the right way - pointing to a class in my project.

我尝试添加一个带有清单文件的文件夹 META-INF,但我没有管理.

I tried adding a folder META-INF with manifest file but I didn't manage.

有没有办法在 NetBeans 中手动执行此操作,因为我发现如果在清单中添加缺少的 Main 类,一切正常.

Is there a way to do this manually in NetBeans, because I found that if I add the missing Main class in the manifest, everything works.

(我想我遇到了某种错误...)

(I suppose I hit some sort of bug...)

//我追求的结果是我希望使用 NetBeans 构建创建的 jar 可以使用命令执行:

//edit: The result I'm after is that I want the jar that is created with the build of NetBeans to be executable with command:

引用自 Sun 文档:

在清单文件中设置 Main-Class 后,您可以从命令行运行应用程序:java -jar app.jar

When the Main-Class is set in the manifest file, you can run the application from the command line: java -jar app.jar

推荐答案

我将总结提出的解决方案以及对我有帮助的解决方案!

I'm going to make a summary of the proposed solutions and the one that helped me!

阅读此错误报告后:NetBeans 6.8 为 Java 库项目创建 jar 的方式中的错误.>

After reading this bug report: bug in the way NetBeans 6.8 creates the jar for a Java Library Project.

  1. 在我的项目根目录中创建一个 manifest.mf 文件

  1. Create a manifest.mf file in my project root

编辑 manifest.mf.我的看起来像这样:

Edit manifest.mf. Mine looked something like this:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 16.3-b01 (Sun Microsystems Inc.)
Main-Class: com.example.MainClass
Class-Path: lib/lib1.jar lib/lib2.jar

  • 打开文件/nbproject/project.properties

  • Open file /nbproject/project.properties

    添加行

    manifest.file=manifest.mf

    清理 + 构建项目

    现在 .jar 已成功构建.

    Now the .jar is successfully build.

    非常感谢vkraemer

    这篇关于如何在 NetBeans 项目生成的 jar 中的清单文件中设置 Main 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆