java – 在jar中解析manifest.mf文件的条目的正确方法是什么?
发布时间:2020-12-14 05:04:31 所属栏目:Java 来源:网络整理
导读:包含在许多 Java jar中的manifest.mf包含看起来像邮件头的头.参见示例[*] 我想要一些可以将这种格式解析成键值对的东西: MapString,String manifest = mystery-parse-function(new File("manifest.mf")); 我已经对“解析manifest.mf”“manifest.mf格式”等
包含在许多
Java jar中的manifest.mf包含看起来像邮件头的头.参见示例[*]
我想要一些可以将这种格式解析成键值对的东西: Map<String,String> manifest = <mystery-parse-function>(new File("manifest.mf")); 我已经对“解析manifest.mf”“manifest.mf格式”等进行了谷歌搜索,并且发现了很多关于标题含义的信息(例如在OSGI捆绑包,标准Java jar等),但这不是什么我在找. 看一些示例manifest.mf文件,我可能会实现一些东西来解析它(反向工程的格式),但我不知道我的实现是否真的是正确的.所以我也没有找别人快速抛出解析功能,因为它遇到同样的问题). 对我的问题的一个很好的答案可以指出我的格式的规范(所以我可以写我自己正确的解析函数).最好的答案是指我已经有一个正确的实现的现有开源库. [*] = https://gist.github.com/kdvolder/6625725 解决方法
MANIFEST.MF文件可以用
Manifest类读取:
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |