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

前端 未结 1 1850
一个人的身影
一个人的身影 2021-01-08 01:36

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

相关标签:
1条回答
  • 2021-01-08 02:07

    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
    
    0 讨论(0)
提交回复
热议问题