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

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

Automatically Creating Manifest File with Eclipse for JAR Files

javaeclipsejarmanifest

提问by

How can I create a Manifestfile for a group of JAR file I have already created.

如何Manifest为我已经创建的一组 JAR 文件创建一个文件。

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?

回答by Jo?o Silva

A default manifest file is created when you create a jarusing jar cf jar-file inputs-files.

创建jarusing时会创建默认清单文件jar cf jar-file inputs-files

回答by Michael Borgwardt

A manifest is a simple text file, Sun's tutorialdescribes its contents in detail.

清单是一个简单的文本文件,Sun 的教程详细介绍了其内容。

All you have to do is create such a file and update the JAR fileswith this command:

您所要做的就是创建这样一个文件并使用以下命令更新 JAR 文件

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

回答by Joonas Pulakka

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. This doesn't work for signed JARs, of course.

清单文件是文本文件。由于 JAR 文件实际上是 ZIP 文件,您还可以使用7-zip 之类的工具添加清单。当然,这不适用于已签名的 JAR。

回答by Nate

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。

回答by JuanZe

With Ant you can use jartask. It has a manifestoption to specify all the attributes yo need to include. Something like this:

使用 Ant,您可以使用jartask。它有一个清单选项来指定您需要包含的所有属性。像这样的东西:

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

回答by JuanZe

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. 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). 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. Select the Manifest file you just added to your project. Make sure the Manifest has the path to your Main class (e.g. com.company etc). For example this is how a basic Manifest file would look like:

它只包括行Manifest-Version: 1.0。我还没有想出如何让它添加我的 Main 类,所以我创建了我自己的 MANIFEST.MF 文件,将它添加到我的项目主目录(不是 src 而不是 lib)。然后,当我转到“导出”>“JAR 文件”时,一直点击“下一步”直到最后(不要太早点击“完成”),然后单击从项目中选择清单的选项。选择您刚刚添加到项目中的清单文件。确保 Manifest 有你的 Main 类的路径(例如 com.company 等)。例如,这是一个基本的 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. My above method works well.

我尝试了@Michael 的方法,但最终我的 JAR 中没有任何类文件,只有清单文件.. 哦,好吧。我上面的方法效果很好。

回答by Reedyseth

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 文档

回答by Kenneth V

Using Eclipse, on the JAR Manifest Specificationpage at the bottom, there is a text box where you can browse for your Mainclass. Browse to your Mainclass and click Finish. This generates the manifest WITH the main class information allowing the jar file to be executed. -Dev On

使用 Eclipse,在底部的JAR Manifest Specification页面上有一个文本框,您可以在其中浏览Main类。浏览到您的Main类并单击Finish。这将生成带有主类信息的清单,允许执行 jar 文件。- 开发