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
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.
a) Reorder entries in JAR file using jar
tool:
test.jar
file to directory foo
MANIFEST.MF
file one level above directory foo
and remove META-INF
directoryfoo
directory run command: jar cvfm ../test.jar ../MANIFEST.MF .
foo
you will get fixed JAR file with correct entries orderb) use JarFile
instead of JarInputStream
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/). (...)