The manifest.mf contained in many Java jars contains headers which look much like email headers. See example [*]
I want something that can parse this format into key value pairs:
Map<String, String> manifest = <mystery-parse-function>(new File("manifest.mf"));
I have googled around a bit for "parse manifest.mf" "manifest.mf format" etc. and I find plenty of information about the meaning of the headers (e.g. in OSGI bundles, standard Java jars, etc.) but that's not what I'm looking for.
Looking at some example manifest.mf files I could probably implement something to parse it (reverse engineer the format) but I won't know if my implementation is actually correct. So I'm also not looking for someone else's quickly thrown together parse function as it suffers the same problem).
A good answer to my question could point me to a specification of the format (so I can write my own correct parse function). The best answer points me to an existing open-source library that already has a correct implementation.
[*] = https://gist.github.com/kdvolder/6625725
The manifest is a special file that can contain information about the files packaged in a JAR file. By tailoring this "meta" information that the manifest contains, you enable the JAR file to serve a variety of purposes.
You use the m command-line option to add custom information to the manifest during creation of a JAR file. This section describes the m option. The Jar tool automatically puts a default manifest with the pathname META-INF/MANIFEST. MF into any JAR file you create.
NetBeans IDE This was easily brought up in NetBeans by simply using File --> Open File and selecting jdiff. jar to have it displayed as shown in the next screen snapshot.
The manifest file is named MANIFEST. MF and is located under the META-INF directory in the JAR. It's simply a list of key and value pairs, called headers or attributes, grouped into sections.
MANIFEST.MF files can be read with the Manifest class:
Manifest manifest = new Manifest(new FileInputStream(new File("MANIFEST.MF")));
Then you can get all entries by doing
Map<String, Attributes> entries = manifest.getEntries();
And all main attributes by doing
Attributes attr = manifest.getMainAttributes();
My MANIFEST.MF
file is this:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
My code:
Manifest manifest = new Manifest(new FileInputStream(new File("MANIFEST.MF")));
Attributes attr = manifest.getMainAttributes();
System.out.println(attr.getValue("Manifest-Version"));
System.out.println(attr.getValue("X-COMMENT"));
Output:
1.0
Main-Class will be added automatically by build
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With