我试图从清单文件中提取信息以显示在我的jar
文件中的某个方法中,但似乎遇到了一些问题。任何帮助表示赞赏。如何从清单文件中读取元数据
清单文件:
Manifest-Version: 1.0
Created-By: 1.8.0_60 (Oracle Corporation)
Main-Class: com.example.package1.myClass
Name: com/example/package1
Specification-Title: MyPackage
Specification-Version: v1.1
Specification-Vendor: MyCompanyName
Implementation-Title: MP
Implementation-Version: 2015-11-05-C
Implementation-Vendor: MyName
Name: com/example/package2
Specification-Title: MySecondaryPackage
Specification-Version: v2.0
Specification-Vendor: MyCompanyName
Implementation-Title: M2ndP
Implementation-Version: 2015-11-05-C
Implementation-Vendor: MyName
myClass.java:
package com.example.package1;
import com.example.package2;
class myClass {
public static void main(String[] args) {
try {
myClass clz = new myClass();
Thread.sleep(10000); //pause 10 seconnds so we can see what's spit out
} catch (Exception e) {
//excluded in example
}
}
public myClass() {
Package pkg = getClass().getPackage();
if (pkg == null)
System.out.println("No Package Found");
else {
System.out.println("specs: " + pkg.getSpecificationTitle() + " - " + pkg.getSpecificationVersion());
System.out.println("imps: " + pkg.getImplementationTitle() + " - " + pkg.getImplementationVersion());
System.out.println("name: " + pkg.getName());
}
//other code here excluded from example
}
}
输出:
specs: null - null
imps: null - null
name: com.example.package1
那么是什么原因?它看起来像pkg对象被正确定义,但它不读取任何规范或实现属性。
我认为这个问题回答您的问题:http://stackoverflow.com/questions/1272648/reading-my-own-jars-manifest –
@Clayton,谢谢,但我已经去过那里了,试过了。我在那里尝试了几个选择,但没有比我在这里得到的更多。包名称,但只有属性为空值。 –
这些用于从包中检索属性的方法应该可行,否则它们的目的是什么?我只是解释了它们对jar中的manifest.mf文件的功能?我觉得我必须错过简单的东西。这让我疯狂。 –