【问题标题】:What is the proper way to parse the entries of a manifest.mf file in jar?解析 jar 中 manifest.mf 文件条目的正确方法是什么?
【发布时间】:2013-09-24 19:12:15
【问题描述】:

许多 Java jar 中包含的 manifest.mf 包含看起来很像电子邮件标题的标题。参见示例 [*]

我想要一些可以将这种格式解析为键值对的东西:

Map<String, String> manifest = <mystery-parse-function>(new File("manifest.mf"));

我搜索了一些关于“解析 manifest.mf”“manifest.mf 格式”等的信息,并找到了大量关于标头含义的信息(例如,在 OSGI 包、标准 Java jar 等中)但是这不是我要找的。​​p>

查看一些示例 manifest.mf 文件,我可能会实现一些东西来解析它(对格式进行逆向工程),但我不知道我的实现是否真的正确。所以我也不是在寻找其他人的快速组合解析函数,因为它遇到了同样的问题)。

对我的问题的一个好的回答可以为我指明格式规范(这样我就可以编写自己的 正确 解析函数)。最佳答案将我指向一个已经有正确实现的现有开源库。

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

【问题讨论】:

    标签: java parsing jar manifest


    【解决方案1】:

    可以使用Manifest 类读取MANIFEST.MF 文件:

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

    然后你可以通过做得到所有条目

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

    以及所有主要属性

    Attributes attr = manifest.getMainAttributes();
    

    一个工作示例

    我的MANIFEST.MF 文件是这样的:

    Manifest-Version: 1.0
    X-COMMENT: Main-Class will be added automatically by build
    

    我的代码:

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

    输出:

    1.0
    Main-Class will be added automatically by build
    

    【讨论】:

    • 您好我正在尝试使用这种方式来解析 Manifest 文件,但它只填充私有字段 attr 的值。整个属性始终为空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 2023-03-23
    相关资源
    最近更新 更多