Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to parse the entries of a manifest.mf file in jar?

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

like image 254
Kris Avatar asked Sep 19 '13 15:09

Kris


People also ask

What is manifest MF in JAR?

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.

How do I add manifest MF to JAR?

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.

How do I view the manifest of a JAR file?

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.

Where do I put manifest file in JAR?

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.


1 Answers

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();

A working example

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
like image 84
BackSlash Avatar answered Nov 13 '22 17:11

BackSlash