简体   繁体   English

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

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

I have the following problem.我有以下问题。 I have a Java project in my NetBeans IDE 6.8.我的 NetBeans IDE 6.8 中有一个 Java 项目。 When I compile it and it produces a .jar file containing everything possible, the META-INF is not right.当我编译它并生成一个包含所有可能的 .jar 文件时,META-INF 是不正确的。 It doesn't contain the class to be executed - with main() method.它不包含要执行的类 - 使用 main() 方法。

When I click the Run button inside the IDE, everything works.当我单击 IDE 中的运行按钮时,一切正常。 The settings of the project are also set the right way - pointing to a class in my project.项目的设置也设置正确——指向我项目中的一个类。

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 fromSun 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

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在我的项目根目录中创建一个 manifest.mf 文件

  2. Edit manifest.mf.编辑 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打开文件 /nbproject/project.properties

  4. Add line添加行

    manifest.file=manifest.mf

  5. Clean + Build of project清理 + 构建项目

Now the .jar is successfully build.现在 .jar 已成功构建。

Thank you very much vkraemer非常感谢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 网站/问题跟踪器上注册并更新问题并添加您的“投票”。

It is simple.很简单。

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

Thats it.就是这样。 Hope this helps.希望这可以帮助。

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

This is a problem still as of 7.2.1 .从 7.2.1 开始,这仍然是一个问题。 Create a library cause you do not know what it will do if you make it an application & you are screwed.创建一个库,因为如果你把它变成一个应用程序,你不知道它会做什么并且你被搞砸了。

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

After this you can change the main class in properties and it will be reflected in manifest.在此之后,您可以更改属性中的主类,它将反映在清单中。

The real problem is how Netbeans JARs its projects.真正的问题是 Netbeans 如何 JAR 对其项目。 The "Class-Path:" in the Manifest file is unnecessary when actually publishing your program for others to use.在实际发布您的程序供其他人使用时,Manifest 文件中的“Class-Path:”是不必要的。 If you have an external Library added in Netbeans it acts as a package.如果您在 Netbeans 中添加了一个外部库,它将充当一个包。 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.我建议你使用像 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

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

I read and read and read to figure out why I was getting a class not found error, it turns out the manifest.mf had an error in the line:我读了又读又读来弄清楚为什么我得到一个找不到类的错误,结果manifest.mf在行中有一个错误:

Main-Class: com.example.MainClass

I fixed the error by going to Project Properties dialog (right-click Project Files ), then Run and Main Class and corrected whatever Netbeans decided to put here.我通过转到Project Properties对话框(右键单击Project Files )修复了错误,然后转到RunMain Class并更正了 Netbeans 决定放在这里的任何内容。 Netbean inserted the project name instead of the class name. Netbean 插入了项目名称而不是类名称。 No idea why.不知道为什么。 Probably inebriated on muratina...可能是陶醉在 muratina 上...

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 package is your package and myClassName is the class with the main(String[] args) method.其中package是您的包,而myClassName是具有main(String[] args)方法的类。

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.提取要添加到项目中的 jar 文件。

  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.使用 WinRAR 打开主项目 jar 文件(dist/file.jar)。

  4. paste that folder or package in the main project jar file.将该文件夹或包粘贴到主项目 jar 文件中。

  5. Those packages work 100% in your project.这些包在您的项目中 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.在我的例子中,lib 文件夹存在于 dist(主 jar 文件)文件夹之外。
  • we need to move lib folder in dist folder.then we set class path from manifest.mf file of main jar file.我们需要移动 dist 文件夹中的 lib 文件夹。然后我们从主 jar 文件的 manifest.mf 文件设置类路径。

    Edit the manifest.mf And ADD this type of line编辑 manifest.mf 并添加这种类型的行

  • Class-Path: lib\\foldername\\jarfilename.jar lib\\foldername\\jarfilename.jar类路径: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 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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