Error: Invalid or corrupt jarfile
I was creating a Jar via the Java API’s and I couldn’t get it to run my main class:
$ java -jar foo.jar Error: Invalid or corrupt jarfile foo.jar
Running it via the class path worked fine:
$ java -cp foo.jar Bar Hello world!
So now I knew it was something to do with the manifest file but it wasn’t being caused by
- The 65535 file limit (See Zip64 and Java 7 ).
- The 72 bytes limit per line
- Missing newline at the end of the Main-Class (Displays "no main manifest attribute, in foo.jar"
- A blank line before Main-Class (Displays "Error: An unexpected error occurred while trying to open file foo.jar"
$ unzip -lv foo.jar
Archive: foo.jar
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
75 Defl:N 75 0% 2014-02-17 16:01 b1eac370 META-INF/MANIFEST.MF
825 Defl:N 464 44% 2014-02-17 16:01 942f187c Working.class
$ unzip -lv foo.jar
Archive: foo.jar
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
75 Defl:N 75 0% 2014-02-17 16:01 b1eac370 /META-INF/MANIFEST.MF
825 Defl:N 464 44% 2014-02-17 16:01 942f187c Failing.class
So don't prefix the META-INF folder with a slash! Also note it is case sensitive!