IOException: 'Invalid header field; when creating .jar file with manifest

六月ゝ 毕业季﹏ 提交于 2020-06-11 17:08:32

问题


When I type jar cvfm file_name.jar manifest.txt *.class in command prompt I get this error:

java.io.IOException: invalid header field
    at java.util.jar.Attributes.read(Attributes.java:410)
    at java.util.jar.Manifest.read(Manifest.java:199)
    at java.util.jar.Manifest.<init>(Manifest.java:69)
    at sun.tools.jar.Main.run(Main.java:172)
    at sun.tools.jar.Main.main(Main.java:1177)

I've never gotten this error before and I can't find anything on it, what does it mean?


回答1:


Check the name of a header variable in the MANIFEST file. MANIFEST file is not correct.

This tutorial will help to identify the MANIFEST file format and related things, http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html




回答2:


Be careful about the order of the parameters:

1) jar cvmf manifest.txt some.jar package/*class
2) jar cvfm some.jar manifest.txt package/*class



回答3:


In case you land here and tried everything and still isn't getting rid of the problem, take a look if there are any accidental tabs instead of 4 spaces for indentation in the MANIFEST file.

I was using a maven pom.xml file to auto-generate the MANIFEST and the property line was too big, spanning multiple lines like this:

<Extension-List> item1 item2 item3 item4
                item5 item6 item7 item8    <--- these lines are idented with tabs
                item9 item10 item11 item12 </Extension-List>

this was corrupting the MANIFEST file in a very hard to see way.




回答4:


Your Manifest file has to follow the required format. If you're generating the file yourself, then you should be able to figure out where you got the syntax wrong. If the manifest file was generated by some tool or process, however, you'll have to check the tool's documentation to see whether you ran into a bug in the tool.




回答5:


Read carefully the first line of your manifest, for example :

Main-Class: main.HelloWorld

I had this error because I added a space like this "Main-Class :"

Maybe it is something similar.




回答6:


My problem was that I had loads of dependencies on one line so I split it up using a simple Find and Replace where each JAR was on a separate line. It turns out that if you have something like:

Class-Path: dependencies/org.apache.commons.cli_1.4.jar (note the space at the end)
dependencies/org.apache.log4j_1.2.15.v201012070815.jar (note the space at the end)

You need to make sure that the line doesn't end with a space, and that the new line begins with a space, like so:

Class-Path: dependencies/org.apache.commons.cli_1.4.jar
 dependencies/org.apache.log4j_1.2.15.v201012070815.jar



回答7:


This problem can also occur if your MANIFEST.MF file begins with some non-printable characters. Tools like notepad and wordpad don't let you see them, so you'll swear you're file looks right and that all your headers are valid.

In my case I got the message:

java.io.IOException: invalid header field name: Built-By

The clue if you are having this situation are those characters occurring before the valid Built-By header. Those are non-printable characters that somehow got into your MANIFEST.MF file. The other clue is to open a command line, navigate to your MANIFEST.MF file and do

>cat MANIFEST.MF
□Build-BY: TSRUT
Bundle-Description: Fragment Controller.
Bundle-License:   http://www.apache.org/licenses/LICENSE-2.0.txt

Notice that box character before your 1st header at the beginning of the file. If that's the case, this is the solution to your problem:

Open up your favorite editor, manual select the entire file (do not use the select-all command, as that will also select those hidden characters). Close the file. Open a new instance of your editor, paste your content, and save the new file back to your original MANIFEST.MF file. Select yes to overwrite. Then verify that those hidden character were removed from the beginning of the file.




回答8:


if your manifest file is having some additional empty lines will also cause this error . Make sure to remove the additional lines.



来源:https://stackoverflow.com/questions/18907695/ioexception-invalid-header-field-when-creating-jar-file-with-manifest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!