Updating jar manifest file - java.io.IOException: invalid manifest format

13,959

Solution 1

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

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.

Solution 2

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 FilesToadd Then 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 Jarfilename now 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:

1.You may have not left space between the name and value of any section in manifest file,
like Version:1.1 is inavalid section instead write Version: 1.1 that space between colon and 1.1 really matters a lot.

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.

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

4.Your text file for manifest must use UTF-8 encoding otherwise you may get into some trouble.

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

Manifest-Version: 2.1

Created-By: UselessCoder

Package-Name: calculator

Class-Name: calculator.Calculator.java

Main-Class: calculator.Calculator

Solution 3

java.io.IOException: invalid manifest format error is also thrown when,

unwanted empty lines are present in manifest file.

For example:

Before manifest.txt

Manifest-Version: 1.0

Main-Class: Sample

After manifest.txt

Manifest-Version: 1.0
Main-Class: Sample
Share:
13,959
rrirower
Author by

rrirower

Masters rower and former ACE personal trainer and competitive bodybuilder writing software as an individual developer. Currently, 7,804,467 meters logged on the erg. Gort, Klaatu, Barada Nikto...

Updated on June 15, 2022

Comments

  • rrirower
    rrirower almost 2 years

    I need to update the manifest in my jar file that I create (export) within Eclipse. I've tried to follow this explanation 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.mf to add new attributes? An example would be most helpful.