Search code examples
javajarmanifest

JarInputStream retuns null for existing Manifest file


I have a problem when using JarInputStream to get the Manifest of a (fully) operating .jar file, I get null pointer. Tried a few bit different files and no exception is thrown. Note : This file comes from a code generator. Has the following structure (hope this shape help you understand it):

|[+]test.jar
  |[+]META-INF
      |MANIFEST.MF
  |[+]mypackage
      |MyClass.class
|text.txt

Above is some part of the class (Note : This class is not located inside the .jar)

/*...code...**/
File jar = new File("resources/test.jar");
FileInputStream fis = new FileInputStream(jar);
JarInputStream jis = new JarInputStream(fis);
Manifest jarManifest = jis.getManifest();
Attributes jarManifestAttributes = jarManifest.getMainAttributes(); //line 25
String mainClass = jarManifestAttributes.getValue("Main-Class");
/*...code...**/

When running this code above, the below exception is throwed :

Exception in thread "main" java.lang.NullPointerException
at crypter.LoadJar.main(LoadJar.java:25)

So jarManifest has null value.

EDIT1 MANIFEST.MF

Manifest-Version: 1.0
Main-Class: mypackage.MyClass
Permissions: all-permissions

Name: text.txt

Name: mypackage/MyClass.class

EDIT2

jar -tvf reverse_tcp2.jar 
    37 Sat Jan 07 11:31:14 EET 2017 text.txt
     0 Sat Jan 07 11:31:14 EET 2017 mypackage/
  8513 Sat Jan 07 11:31:14 EET 2017 mypackage/MyClass.class
     0 Sat Jan 07 11:31:14 EET 2017 META-INF/
   145 Sat Jan 07 11:31:14 EET 2017 META-INF/MANIFEST.MF

Solution

  • It's a bug in JarInputStream that MANIFEST.MF must be in the first entries in a ZIP file, like this:

    $ jar -tvf example-correct-archive.jar
      623 Thu Jun 13 12:13:42 CEST 2019 META-INF/MANIFEST.MF
        0 Thu Jun 13 12:13:42 CEST 2019 META-INF/
        ...
    

    In your case, you have 3 other files before that entries.

    Workarounds

    a) Reorder entries in JAR file using jar tool:

    • unpack test.jar file to directory foo
    • move existing MANIFEST.MF file one level above directory foo and remove META-INF directory
    • inside foo directory run command: jar cvfm ../test.jar ../MANIFEST.MF .
    • outside of foo you will get fixed JAR file with correct entries order

    b) use JarFile instead of JarInputStream

    More info

    See JDK-8031748 Clarify jar entry orders in a jar file:

    From the beginning, the jar file has an “undocumented” assumption that the MANIFEST.MF file and signature-related files (block and SF) should appear at the beginning (except for directory entries, say, META-INF/). (...)