Java 如何在 NetBeans 项目生成的 jar 中的清单文件中设置 Main 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2848642/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:36:04  来源:igfitidea点击:

How to setup Main class in manifest file in jar produced by NetBeans project

javanetbeansmanifest

提问by Leni Kirilov

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.

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

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.

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

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

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

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.

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

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

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

//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:

//编辑:我想要的结果是我希望使用 NetBeans 构建创建的 jar 可以使用命令执行:

Quote from Sun Documentation:

引用自Sun 文档

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

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

采纳答案by Leni Kirilov

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

我将总结提出的解决方案和帮助我的解决方案!

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

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

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

  2. 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
    
  3. Open file /nbproject/project.properties

  4. Add line

    manifest.file=manifest.mf

  5. Clean + Build of project

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

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

    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
    
  3. 打开文件 /nbproject/project.properties

  4. 添加行

    manifest.file=manifest.mf

  5. 清理 + 构建项目

Now the .jar is successfully build.

现在 .jar 已成功构建。

Thank you very much vkraemer

非常感谢vkraemer

回答by Ravindra Gullapalli

It is simple.

很简单。

  • Right click on the project
  • Go to Properties
  • Go to Runin Categoriestree
  • Set the Main Classin the right side panel.
  • Build the project
  • 右键单击项目
  • 转到属性
  • 转到在类别树中运行
  • 在右侧面板中设置主类
  • 构建项目

Thats it. Hope this helps.

就是这样。希望这可以帮助。

回答by vkraemer

It looks like you are running into a bug in the way NetBeans 6.8 creates the jar for a Java Library Project.

看起来您在 NetBeans 6.8 为 Java 库项目创建 jar 的方式中遇到错误

The issue implies that there is a work-around.

这个问题意味着有一个变通方法。

I have not been able to verify that with NB 6.8 and/or NetBeans 6.9-dev...

我无法使用 NB 6.8 和/或 NetBeans 6.9-dev 验证...

You may want to register with the NetBeans.org website/issue tracker and update the issue and add your 'vote'.

您可能希望在 NetBeans.org 网站/问题跟踪器上注册并更新问题并添加您的“投票”。

回答by Tomas Zezula

Adding manifest.file=manifest.mf into project.properties and creating manifest.mf file in the project directory works fine in NB 6.9 and should work also in NB 6.8.

将 manifest.file=manifest.mf 添加到 project.properties 并在项目目录中创建 manifest.mf 文件在 NB 6.9 中工作正常,在 NB 6.8 中也应该工作。

回答by Jeff Palmer

This is a problem still as of 7.2.1 . Create a library cause you do not know what it will do if you make it an application & you are screwed.

从 7.2.1 开始,这仍然是一个问题。创建一个库,因为如果你把它变成一个应用程序,你不知道它会做什么并且你被搞砸了。

Did find how to fix this though. Edit nbproject/project.properties, change the following line to false as shown:

不过确实找到了解决方法。编辑nbproject/project.properties,将以下行更改为 false,如下所示:

mkdist.disabled=false

mkdist.disabled=false

After this you can change the main class in properties and it will be reflected in manifest.

在此之后,您可以更改属性中的主类,它将反映在清单中。

回答by Samuel Ventura

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

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

回答by Samuel Ventura

The real problem is how Netbeans JARs its projects. The "Class-Path:" in the Manifest file is unnecessary when actually publishing your program for others to use. If you have an external Library added in Netbeans it acts as a package. I suggest you use a program like WINRAR to view the files within the jar and add your libraries as packages directly into the jar file.

真正的问题是 Netbeans 如何 JAR 对其项目。在实际发布您的程序供其他人使用时,Manifest 文件中的“Class-Path:”是不必要的。如果您在 Netbeans 中添加了一个外部库,它将充当一个包。我建议你使用像 WINRAR 这样的程序来查看 jar 中的文件,并将你的库作为包直接添加到 jar 文件中。

How the inside of the jar file should look:

jar 文件的内部应该是什么样子:

MyProject.jar

    Manifest.MF
         Main-Class: mainClassFolder.Mainclass

    mainClassFolder
         Mainclass.class

    packageFolder
         IamUselessWithoutMain.class

回答by githeko

I read and read and read to figure out why I was getting a class not found error, it turns out the manifest.mfhad an error in the line:

我阅读并阅读并阅读以找出为什么我会收到一个未找到类的错误,结果manifest.mf在该行中有一个错误:

Main-Class: com.example.MainClass

I fixed the error by going to Project Propertiesdialog (right-click Project Files), then Runand Main Classand corrected whatever Netbeans decided to put here. Netbean inserted the project name instead of the class name. No idea why. Probably inebriated on muratina...

我通过转到Project Properties对话框(右键单击Project Files)修复了错误,然后转到RunMain Class并更正了 Netbeans 决定放在这里的任何内容。Netbean 插入了项目名称而不是类名称。不知道为什么。可能是陶醉在 muratina 上...

回答by madamis confidential

Don't hesitate but look into your project files after you have built your project for the first time. Look for a manifest file and choose open with notepad.

在您第一次构建您的项目后,请不要犹豫,但请查看您的项目文件。查找清单文件并选择用记事本打开。

Add the line:

添加行:

Main-Class: package.myMainClassName

Where packageis your package and myClassNameis the class with the main(String[] args)method.

package你的包在哪里,myClassName是带有main(String[] args)方法的类。

回答by Parm sohi

Brother you don't need to set class path just follow these simple steps (I use Apache NetBeans)

兄弟你不需要设置类路径只需按照这些简单的步骤(我使用Apache NetBeans)

Steps:

脚步:

  1. extract the jar file which you want to add in your project.

  2. only copy those packages (folder) which you need in the project. (do not copy manifest file)

  3. open the main project jar file(dist/file.jar) with WinRAR.

  4. paste that folder or package in the main project jar file.

  5. Those packages work 100% in your project.

  1. 提取要添加到项目中的 jar 文件。

  2. 只复制您在项目中需要的那些包(文件夹)。(不要复制清单文件)

  3. 使用 WinRAR 打开主项目 jar 文件(dist/file.jar)。

  4. 将该文件夹或包粘贴到主项目 jar 文件中。

  5. 这些包在您的项目中 100% 工作。

warning: Do not make any changes in the manifest file.

警告:请勿在清单文件中进行任何更改。

Another method:

另一种方法:

  • In my case lib folder present outside the dist(main jar file) folder.
  • we need to move lib folder in dist folder.then we set class path from manifest.mf file of main jar file.

    Edit the manifest.mf And ADD this type of line

  • Class-Path: lib\foldername\jarfilename.jar lib\foldername\jarfilename.jar
  • 在我的例子中,lib 文件夹存在于 dist(主 jar 文件)文件夹之外。
  • 我们需要移动 dist 文件夹中的 lib 文件夹。然后我们从主 jar 文件的 manifest.mf 文件设置类路径。

    编辑 manifest.mf 并添加这种类型的行

  • 类路径:lib\foldername\jarfilename.jar lib\foldername\jarfilename.jar

Warning:lib folder must be inside the dist folder otherwise jar file do not access your lib folder jar files

警告:lib 文件夹必须在 dist 文件夹内,否则 jar 文件不会访问您的 lib 文件夹 jar 文件