Java – parse manifest.xml in jar What is the correct method for entries in MF files?

Manifest.jar included in many Java jars MF contains headers that look like 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 "parse manifest. MF" and "manifest. MF format" and found a lot of information about the meaning of the title (such as OSGi bundle, standard Java jar, etc.), but this is not what I'm looking for

Look at some examples of manifest MF file, I may implement something to parse it (reverse engineering format), but I don't know whether my implementation is really correct So I didn't ask others to throw the parsing function quickly, because it encountered the same problem)

A good answer to my question can point out the specification of my format (so I can write my own correct parsing function) The best answer is that I already have an existing open source library with the correct implementation

[*] = https://gist.github.com/kdvolder/6625725

Solution

MANIFEST. MF files can be read with the manifest class:

Manifest manifest = new Manifest(new FileInputStream(new File("MANIFEST.MF")));

Then you can do all the input

Map<String,Attributes> entries = manifest.getEntries();

And all the main properties

Attributes attr = manifest.getMainAttributes();

A working example

My manifest The MF file is as follows:

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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>