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

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?

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

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

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.

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.

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.

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

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.

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

Related

Can't update manifest.mf inside .jar: line too long error

I have one .jar file with only MANIFEST/MANIFEST.MF file inside it. I want to extend classpath in this file. Already googled on how to do this and found this:
Extract MANIFEST/MANIFEST.MF - jar xf myfile.jar
MANIFEST/MANIFEST.MF
Edit manifest file
Update it inside .jar - jar umf myfile.jar MANIFEST/MANIFEST.MF
The problem is that I get this error:
java.io.IOException: line too long
at java.util.jar.Attributes.read(Attributes.java:379)
at java.util.jar.Manifest.read(Manifest.java:199)
at java.util.jar.Manifest.<init>(Manifest.java:69)
at sun.tools.jar.Main.update(Main.java:652)
at sun.tools.jar.Main.run(Main.java:271)
at sun.tools.jar.Main.main(Main.java:1288)
Google says that maximum line length of manifest is 72 - OK. Tried not to edit extracted manifest file. When I try to import the same file I just extracted I get the same error. Maybe there's a tool that formats manifest files in order to make them "valid"?
Thank you in advance.
The update command should be:
jar umf MANIFEST/MANIFEST.MF myfile.jar
Use the below steps to avoid the issue
(1) use separate lines, to avoid too long a line for java package name lists
(2) (Space at the end of each line and one at the beginning) . Type a preceding space before each following lines, for example: Class-Path: ...jar ...jar ...jar

ant puts new line at end of manifest

I have a jar that uses some of the apache tomcat library jars and thus has the following manifest.
Manifest-Version: 1.0
Main-Class: foo
Class-Path: foo.jar foo_utils.jar . /opt/apache-tomcat/
lib/websocket-api.jar /opt/apache-tomcat/lib/tomcat7-websocket.jar /o
pt/apache-tomcat/lib/tomcat-util.jar /opt/apache-tomcat/lib/tomcat-co
yote.jar /opt/apache-tomcat/bin/tomcat-juli.jar
The jar is able to find foo.jar and foo_utils.jar without a problem.
It can't find any of the jars in the /opt/apache-tomcat/lib folder. I have checked the path and made sure that it is a valid path to the jars I want and is.due to the ant build puts a new line at the end of the classpath last line and thus messes with the line being parsed properly.
How may I have ant not put a new line at the end of my manifest class path file?
The JAR File Specification says (emphasis mine):
Class-Path :
The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs.
The presence of a newline at the end of the MANIFEST.MF is not relevant.
Can you check if your manifest file is ending with a new line or carriage return? As per https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html - the last line will not be parsed properly if it does not end with a new line or carriage return.
I'm not sure but that dot between foo_utils.jar and /opt/apache-tomcat/ seems strange to me. Is there a reason for this dot?

An unexpected error occurred while trying to open jar

I have a spring batch project exported as a runable Jar file, it has 4 main methods, only one main is working, the other main needs referenced jar jai-codec-1.1.3.jar, but when I put the jar in the classpath, the first main also could not working and it gives error message "An unexpected error occurred while trying to open jar"
here is my mainfest file:
Manifest-Version: 1.0 Class-Path: JARS/spring-jdbc-3.1.1.RELEASE.jar
JARS/spring-context-3.2.3.RELEASE.jar JARS/log4j-1.2.14.jar
JARS/spring-tx-3.2.3.RELEASE.jar JARS/spring-core-3.2.3.RELEASE.jar
JARS/spring-beans-3.2.3.RELEASE.jar JARS/commons-logging-1.1.1.jar
JARS/spring-aop-3.2.3.RELEASE.jar JARS/aopalliance-1.0.jar
JARS/spring-expression-3.2.3.RELEASE.jar JARS/commons-dbcp-1.4.jar
JARS/commons-pool-1.5.4.jar JARS/ojdbc6-11.2.0.3.jar
JARS/commons-lang3-3.0.jar JARS/itextpdf-5.5.1.jar
JARS/jdom2-2.0.5.jar JARS/jai-codec-1.1.3.jar
I found out that it is not because the specific jar file, it is because the length of the classpath is exceeded the limit, is there a way to put more jars in the MAINFEST file classpath?
also I have defined all #autowired service and component classes but it still not working in jar, it only works when I defined those as bean in spring-config.
Anyone can Help me to figure out why?
This error may indicate (in a very unclear way though) an error in MANIFEST.MF. This can be emulated by an invalid empty line in the middle of the file or by lines too long.
MANIFEST.MF is extremely manual-edit-unfriendly:
It must have a final empty line (or final line terminator in other words),
its lines must be 72 bytes (not chars, see the comment) long at most (I bet this was your true problem, you just fixed symptoms)
and continuation line must start with space...
and on top of it, the classpath entries must be URLs, not file paths.
More about its specification can be found here: https://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html
Problem solved. I am not sure if it is the limitation of classpath size in MAINFEST file, but it really matters when it was too long, I changed all jar name as 1, 2 , 3... and be able to put all jars in the classpath, and it works. Please leave your comments if you have different opinion on this, Thanks.
I will add that Properties in Manifest MUST NOT be dot separated, either dash or no separator: "myProperty" will work, "my-property" will work too, but "my.property" will fail with "Error: An unexpected error occurred while trying to open file ..." with no explanation at all.
Although "my.property" would work as a Java property in some standalone application, it just breaks the launch of the embedded application inside a JAR when the Manifest is read.

nokia error code 217 - jar manifest end of line

I have developed a J2ME application with J2ME Polish. Nokia now validate contents that will be submitted to the store, the challenge is that during validation of my JAR and JAD files, I get Error 217 - The JAR manifest does not end with a new line
I checked my JAR manifest and finds out that it is ending with a new line. Is anyone having any suggestions or solutions?
Double check that there are no invisible characters like whitespace or tab at the last line of the manifest.
If manifest has a newline indeed, error message apparently means a bug in this Nokia device.
Still, it is possible that your manifest has something wrong and that Nokia only used incorrect message to indicate some other problem in it. Getting messages like this, make sure that your manifest conforms to respective sections of JAR file Specification (available online), particularly:
Manifest Specification
Notes on Manifest and Signature Files
First I'd check is that manifest lines do not exceed "72 bytes (not characters), in its UTF8-encoded form".
Another thing worth trying is to experiment with various styles of line endings of those specified: CR LF | LF | CR. In your IDE / build there could be a setting to manage that, look for something called like "DOS / Unix / Mac line breaks". Specification states that all these are OK, but you better account for a chance of bug in the device not being able to recognize particular style line breaks.
Also consider checking / asking at Nokia forums for this might be a known issue with particular device.
Finally got this, i used the jar tool provided by the java sdk to extract the midlet from the jar file like this:
jar xf myjarfile.jar META-INF/MANIFEST.MF
then edited the file in a text editor and updated the manifest in the jar file
jar umf META-INF/MANIFEST.MF myjarfile.jar
Thanks for suggestions guys.
optionally you can open your jar file using winrar software then you can access the manifest from the META - INI file.from this you can edit it using the text editor after which you can save CAUTION:after saving note that the size is going to change so make sure you check the size of the jar after you have edited , by right clicking then view properties.check the size then change it accordingly to jad file by opening your jad file in a text editor
thats it:-0

invalid header file while using jar for archiving

When i use this cmd line :
jar cmf arshad.mf ars.jar *.class
i get this error :
invalid header field name:Manifest-version
This is my manifest file :
Manifest-Version: 1.0
Main-Class:t
i made the manifest file with notepad in UTF-8 encoding - is there any problem with the manifest ?
Add a space after the colons:
Manifest-Version: 1.0
Main-Class: t
Yes,it is true.
A common mistake people make when writing their manifest files for jar's is that they don't put spaces after their colons. I don't know, based on what you wrote here, if that's it or not but give it a try.
Example:
Main-Class:someClass //wrong
Main-Class: someClass //correct
Also, your manifest file must be saved as UTF-8. You're not necessarily safe writing it on MS Notepad and saving as UTF-8 encoding. This post has some good details:
Invalid Header Field Name when adding manifest to JAR using Eclipse
Notepad adds some bytes onto the front to broadcast the endian-ness of the data. This will break the jar command. A decent solution is to not use Notepad and download Notepad++. In Notepad++ you select 'encoding' from the top bar, and select 'UTF-8 Without BOM'. Saving your manifest file with this setting applied should solve the problem. If there are no others.

Categories

Resources