简体   繁体   English

使用 Eclipse 为 JAR 文件自动创建清单文件

[英]Automatically Creating Manifest File with Eclipse for JAR Files

How can I create a Manifest file for a group of JAR file I have already created.如何为我已经创建的一组 JAR 文件创建Manifest文件。

I used Eclipse to create my JAR file.我使用 Eclipse 来创建我的 JAR 文件。

Is there any better way of creating a JAR file that has a Manifest ?有没有更好的方法来创建具有Manifest的 JAR 文件?

Eclipse provides options to generate a manifest file for the Jar, save that generated manifest into the project, or use a specified file for the manifest. Eclipse 提供了为 Jar 生成清单文件、将生成的清单保存到项目中或使用指定文件作为清单的选项。

I have Eclipse 3.4.2 and it's on the fourth screen in this process:我有 Eclipse 3.4.2,它在这个过程的第四个屏幕上:

Right-click Project -> Export -> Java/JAR file, Next, JAR File Specification, Next, JAR Packaging Options, Next, JAR Manifest Specification.右键Project -> Export -> Java/JAR file,Next,JAR File Specification,Next,JAR Packaging Options,Next,JAR Manifest Specification。

The default is to just generate a default manifest for the JAR, and not to save the generated file back to the project, so if you open up your JAR file, it will have a manifest, but it will just have something like:默认是只为 JAR 生成一个默认清单,而不是将生成的文件保存回项目,所以如果你打开你的 JAR 文件,它会有一个清单,但它只会有如下内容:

Manifest-Version: 1.0

in it.在里面。

If you want to change your existing JARs without re-building them, the easiest way is probably to just do as mad-j suggested and open them with a Zip tool and edit the existing /META-INF/MANIFEST.MF file and save it back into the JAR.如果您想在不重新构建现有 JAR 的情况下更改它们,最简单的方法可能是按照 mad-j 的建议进行操作并使用 Zip 工具打开它们并编辑现有的 /META-INF/MANIFEST.MF 文件并保存它回到 JAR。

I just ran into this problem today.我今天刚遇到这个问题。

Yes, Eclipse will create the Manifest for you, but it's really a joke.是的,Eclipse 会为您创建 Manifest,但这真的是个笑话。

It only includes the line Manifest-Version: 1.0 .它只包括行Manifest-Version: 1.0 I have not figured out how to make it add my Main class, so I created my own MANIFEST.MF file, added it to my project main directory (not src and not lib).我还没有想出如何让它添加我的 Main 类,所以我创建了我自己的 MANIFEST.MF 文件,将它添加到我的项目主目录(不是 src 而不是 lib)。 Then, when I go to Export > JAR File, hit Next all the way to the end (do not hit Finish too early) and click the option to select manifest from project.然后,当我转到导出 > JAR 文件时,一直点击下一步(不要太早点击完成),然后单击从项目中选择清单的选项。 Select the Manifest file you just added to your project.选择您刚刚添加到项目中的清单文件。 Make sure the Manifest has the path to your Main class (eg com.company etc).确保 Manifest 有你的 Main 类的路径(例如 com.company 等)。 For example this is how a basic Manifest file would look like:例如,这是一个基本的 Manifest 文件的样子:

Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: MyPackage.MyClass

I tried @Michael's method but I ended up with no class files left in my JAR and only Manifest files.. oh well.我尝试了@Michael 的方法,但最终我的 JAR 中没有任何类文件,只有清单文件.. 哦,好吧。 My above method works well.我上面的方法效果很好。

A manifest is a simple text file, Sun's tutorial describes its contents in detail.清单是一个简单的文本文件, Sun 的教程详细介绍了其内容。

All you have to do is create such a file and update the JAR files with this command:您所要做的就是创建这样一个文件并使用以下命令更新 JAR 文件

jar cfm <jar-file> <manifest-addition>

With Ant you can use jar task .使用 Ant,您可以使用jar task It has a manifest option to specify all the attributes yo need to include.它有一个清单选项来指定您需要包含的所有属性。 Something like this:像这样的东西:

  <target name="create-my-jar">
     <jar jarfile="foo.jar" basedir="bin/classes">      
        <manifest>
        <attribute name="Class-Path" value="xxx"/>
        <attribute name="Ant-Version" value="${ant.version}"/> 
        <attribute name="Created-By" value="JDK ${java.version} (${java.vendor})"/>
        <attribute name='Specification-Title' value='xxx' />
        <attribute name='Specification-Version' value='xxx' />
        <attribute name='Specification-Vendor' value='xxx' />
        <attribute name='Implementation-Title' value='xxx' />
        <attribute name='Implementation-Version' value='xxx' />
        <attribute name='Implementation-Vendor' value='xxx' />
        <attribute name='Implementation-Vendor-Id' value='xxx' />
        <attribute name='Implementation-Vendor-URL' value='xxx' />
        <attribute name="Built-By" value="${user.name}"/>
        <attribute name='Build-Date' value='xxx'/>
        <attribute name='Build-Time' value='xxx'/>              
        </manifest>
        </jar>
   </target>

当您使用jar cf jar-file inputs-files创建jar时,会创建一个默认清单jar cf jar-file inputs-files

Manifest files are text files.清单文件是文本文件。 And since JAR files are actually ZIP files, you can also add the manifest using a tool like 7-zip .由于 JAR 文件实际上是 ZIP 文件,您还可以使用7-zip 之类的工具添加清单。 This doesn't work for signed JARs, of course.当然,这不适用于已签名的 JAR。

Also you can take a look at this documentation, this helped me to solve the issue using the Eclipse wizard.您也可以查看此文档,这有助于我使用 Eclipse 向导解决问题。

See Eclipse Documentation 请参阅 Eclipse 文档

Using Eclipse, on the JAR Manifest Specification page at the bottom, there is a text box where you can browse for your Main class.使用 Eclipse,在底部的JAR Manifest Specification页面上有一个文本框,您可以在其中浏览Main类。 Browse to your Main class and click Finish .浏览到您的Main类并单击Finish This generates the manifest WITH the main class information allowing the jar file to be executed.这将生成带有主类信息的清单,允许执行 jar 文件。 -Dev On - 开发

A little bit late but thats the most simple/efficient solution:有点晚了,但这是最简单/有效的解决方案:

Rightclick Project->Export->Select in Folder Java "Runnable Jar File"->右键单击项目->导出->在文件夹Java中选择“Runnable Jar File”->
在此处输入图片说明

Next-> Launch Configuration: Select that one you recently used to test your project, leave the rest as default Next-> Launch Configuration:选择你最近用来测试你的项目的那个,其余的保持默认

The manifest has now the relating entry to start the *.jar correct清单现在具有启动 *.jar 正确的相关条目

This is the solution.这就是解决方案。 In the JAR MANIFEST Specification Tab use Generate the Manifest File and use Browse and select a file.在 JAR 清单规范选项卡中,使用生成清单文件并使用浏览和 select 文件。 (You can just use the one generated in your older ZIP). (您可以只使用旧 ZIP 中生成的那个)。 Then generate Jar file.然后生成 Jar 文件。 Afterwards, Edit it to put any value.之后,编辑它以放置任何值。

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

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