更新 jar 清单文件 - java.io.IOException:清单格式无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21223015/
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
Updating jar manifest file - java.io.IOException: invalid manifest format
提问by rrirower
I need to update the manifest in my jar file that I create (export) within Eclipse. I've tried to follow thisexplanation without success. I'm not quite sure what to specify on the command line. The Oracle web site is not too clear. I then found a post on SO that said to extract the manifest.mf file from the jar archive, update it, and add it back to the jar archive. I've tried that too, and it appears to work, however, at runtime, I get java.io.IOException: invalid manifest format
. What is the correct way to update the manifest.mfto add new attributes? An example would be most helpful.
我需要更新我在 Eclipse 中创建(导出)的 jar 文件中的清单。我试图遵循这个解释但没有成功。我不太确定在命令行上指定什么。Oracle 网站不是太清楚。然后我在 SO 上找到了一篇文章,说从 jar 存档中提取 manifest.mf 文件,更新它,然后将其添加回 jar 存档。我也试过了,它似乎有效,但是,在运行时,我得到java.io.IOException: invalid manifest format
. 更新manifest.mf以添加新属性的正确方法是什么?一个例子将是最有帮助的。
采纳答案by rrirower
The links offered by Peter were partially useful. However, I was able to solve this more or less by trial and error. The Oracle docs that explain how to do this need lots of work. They lack a good example of how to proceed. Anyway, for those who run into the same issues, here's what I did. I created a text file (eg. "Manifest.txt") using Notepad that contains the manifest attributes I wanted to add/update. In creating this file, I made sure to add a new line character to the last line by pressing the key on the keyboard. Next, I created a DOS bat file to do the actual modification. Here's what it looked like...
Peter 提供的链接部分有用。但是,我通过反复试验或多或少地解决了这个问题。解释如何执行此操作的 Oracle 文档需要大量工作。他们缺乏如何进行的好例子。无论如何,对于那些遇到相同问题的人,这就是我所做的。我使用记事本创建了一个文本文件(例如“Manifest.txt”),其中包含我想要添加/更新的清单属性。在创建这个文件时,我确保通过按下键盘上的键在最后一行添加一个新行字符。接下来,我创建了一个 DOS bat 文件来进行实际修改。这是它的样子......
echo Updating manifest permissions...
"C:\Program Files\Java\jdk1.7.0_25\bin\jar" -umf "c:\some folder\Manifest.txt" "C:\some folder\jartoupdate.jar"
The order of the jar arguments as they relate to the actual paths that follow on the command line is important. The links from Peter's reply pointed that part out.
jar 参数的顺序很重要,因为它们与命令行上的实际路径相关。彼得回复中的链接指出了这一部分。
回答by Peter
When modifying the contens of a jar you should more look into this direction:
http://docs.oracle.com/javase/tutorial/deployment/jar/update.htmland especially
http://docs.oracle.com/javase/tutorial/deployment/jar/modman.htmlwhich describes the process
of updating the manifest.
在修改 jar 的内容时,您应该更多地研究这个方向:
http: //docs.oracle.com/javase/tutorial/deployment/jar/update.html,尤其是
http://docs.oracle.com/javase/ tutorial/deployment/jar/modman.html描述了
更新清单的过程 。
回答by Iceberg
To update a manifest in a jar file, you found the answer in the oracle docs. Here is another place to see the answer. Assuming you have read access to the directory where the JDK is installed and the documentation was downloaded with it (easy to download the documentation, this is for SE7 http://www.oracle.com/technetwork/java/javase/documentation/java-se-7-doc-download-435117.html):
要更新 jar 文件中的清单,您可以在 oracle 文档中找到答案。这是另一个可以看到答案的地方。假设您对安装 JDK 的目录具有读取权限,并且文档也随它一起下载(易于下载文档,这是针对 SE7 http://www.oracle.com/technetwork/java/javase/documentation/java -se-7-doc-download-435117.html):
go to the [install_directory]/docs. In there is an index.htmlfile.
>>>> e.g. mine for JDK 6 is called C:\my_TOOLS\Java_stuff\jdk_1.6.0_20\docs\index.htmlDrag index.htmlonto a browser page.
That page shows you an overview of the JDK documentation and has many links to useful information.
In the top box's row under the Java Language row, click on JAR. It takes you to the page summarizing the documentation (with links) of the jartool.
In the "JAR Tools" section of that page, click on the reference page link for your platform (you probably want "JAR tool reference page for Windows").That "JAR tool reference page" shows you the detailed documentation for the jar command. This is where you'll see the example "jar umf Manifest.txt my_jar.jar". (Solaris/Linux doesn't use the "-" in front of the args such as "umf".
转到[install_directory]/docs。里面有一个index.html文件。
>>>> 例如我的 JDK 6 被称为C:\my_TOOLS\Java_stuff\jdk_1.6.0_20\docs\index.html将index.html拖到浏览器页面上。
该页面向您展示了 JDK 文档的概述,并提供了许多指向有用信息的链接。
在 Java Language 行下方的顶部框行中,单击 JAR。它会将您带到总结 jartool 文档(带有链接)的页面。
在该页面的“JAR 工具”部分,单击您平台的参考页面链接(您可能需要“Windows 的 JAR 工具参考页面”)。“JAR 工具参考页面”向您展示了 jar 命令的详细文档。您将在此处看到示例“ jar umf Manifest.txt my_jar.jar”。(Solaris/Linux 不使用诸如“umf”之类的参数前面的“-”。
I used it (on Linux) to merge 2 custom Manifest files into the default Manifest and put it in my jar. I do it in a two-step process but would be interested to know of a one-step command to do this. (Using two m's to the jar command causes the second manifest to overwrite the first manifest - not merged.)(Remember each manifest file must end with a blank line.)
我使用它(在 Linux 上)将 2 个自定义 Manifest 文件合并到默认 Manifest 中并将其放入我的 jar 中。我在一个两步过程中完成它,但有兴趣知道一个一步命令来做到这一点。(在 jar 命令中使用两个 m 会导致第二个清单覆盖第一个清单 - 未合并。)(记住每个清单文件必须以空行结尾。)
Manifest.txtcontains "Name: " and "Implementation-Version: "
Manifest.my_app.txtcontains "Main-Class: " and "Class-Path: "
The default manifestcontains "Manifest-Version: " and "Created-By: "
Manifest.txt包括“名称”和“实施-版本:”
Manifest.my_app.txt包含“主类”和“类路径”
的缺省清单中包含“清单-版本:”和“创建,通过: ”
jar cmf my_app.jar Manifest.txt my_main.class my_utils.class
jar umf Manifest.my_app.txt my_app.jar
After this, my META-INF/MANIFEST.MFcontains all the fields from all 3 manifests. I expected them to be appended one to another but the fields are jumbled together. Maybe someone can tell how to order them or straight append. This is the order in which they appear in my current META-INF/MANIFEST.MF.
在此之后,我的META-INF/MANIFEST.MF包含来自所有 3 个清单的所有字段。我希望它们一个一个地附加在一起,但这些字段混杂在一起。也许有人可以告诉如何订购它们或直接追加。这是它们出现在我当前META-INF/MANIFEST.MF 中的顺序。
Manifest-Version: 1.0
Implementation-Title: my_app
Implementation-Version: 1.2.1
Class-Path: ...
Name: My App
Created-By: 1.6.0_20 (Oracle Corporation)
Main-Class: My_App
I hope some of this is useful for you.
我希望其中一些对您有用。
回答by Abhey Rana
As manifest file is contained in META-INF subdirectory of jar file under the name MANIFEST.MF .Whenever you create a jar file for command prompt by the command jar cvf Jarfilename FilesToaddThen a default manifest file is created. One can view this file and get an idea of valid Manifestfile. In order to extract manifest file from jar type following command in cmd jar xvf Jarfilenamenow a META-INF subdirectory will appear in the base directory from here you can view default manifest file. Sometimes while updating manifest file we get java.io.IOException: invalid manifest format.This error comes because of following reasons:
由于清单文件包含在名为 MANIFEST.MF 的 jar 文件的 META-INF 子目录中。每当您通过命令jar cvf Jarfilename FilesToadd为命令提示符创建 jar 文件时,就会创建 一个默认清单文件。可以查看此文件并了解有效的 Manifestfile。为了从 jar 中提取清单文件,请在 cmd jar xvf Jarfilename 中执行以下命令, 现在 META-INF 子目录将出现在基本目录中,您可以从此处查看默认清单文件。有时在更新清单文件时,我们会收到 java.io.IOException: invalid manifest format。此错误是由于以下原因造成的:
1.You may have not left space between the name and value of any section in manifest file,
like Version:1.1is inavalid section instead write Version: 1.1that space between colon and 1.1 really matters a lot.
1.您可能没有在清单文件中任何部分的名称和值之间留下空格,
例如Version:1.1是无效部分,而是写Version: 1.1冒号和 1.1 之间的空格真的很重要。
2.While specifying the main class you might have added .class extension at the end of class name.Simply specify the main class by typing Main-Class: Classname.
2.在指定主类时,您可能在类名的末尾添加了 .class 扩展名。只需键入Main-Class: Classname即可指定主类。
3.You may have not added newline at the end of file.You need not to write \n for specifying newline instead just leave the last line of your manifest file blank that will serve the purpose
3.您可能没有在文件末尾添加换行符。您不需要写 \n 来指定换行符,只需将清单文件的最后一行留空即可
4.Your text file for manifest must use UTF-8encoding otherwise you may get into some trouble.
4.您的清单文本文件必须使用UTF-8编码,否则您可能会遇到一些麻烦。
Finally i am providing an example of what a manifest file must look like. Here package is calculator and the main class is Calculator.java
最后,我提供了一个清单文件必须是什么样子的示例。这里包是计算器,主类是Calculator.java
Manifest-Version: 2.1
清单版本:2.1
Created-By: UselessCoder
创建者:UselessCoder
Package-Name: calculator
包名:计算器
Class-Name: calculator.Calculator.java
类名:calculator.Calculator.java
Main-Class: calculator.Calculator
主类:calculator.Calculator
回答by Parthasarathy S
java.io.IOException: invalid manifest formaterror is also thrown when,
java.io.IOException: invalid manifest format错误时也会抛出,
unwanted empty lines are present in manifest file.
清单文件中存在不需要的空行。
For example:
例如:
Beforemanifest.txt
在manifest.txt之前
Manifest-Version: 1.0
Main-Class: Sample
Aftermanifest.txt
在manifest.txt之后
Manifest-Version: 1.0
Main-Class: Sample