2015-11-05 75 views
2

我試圖從清單文件中提取信息以顯示在我的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對象被正確定義,但它不讀取任何規範或實現屬性。

+0

我認爲這個問題回答您的問題:http://stackoverflow.com/questions/1272648/reading-my-own-jars-manifest –

+0

@Clayton,謝謝,但我已經去過那裏了,試過了。我在那裏嘗試了幾個選擇,但沒有比我在這裏得到的更多。包名稱,但只有屬性爲空值。 –

+0

這些用於從包中檢索屬性的方法應該可行,否則它們的目的是什麼?我只是解釋了它們對jar中的manifest.mf文件的功能?我覺得我必須錯過簡單的東西。這讓我瘋狂。 –

回答

2

所以我終於明白了,並認爲我會分享任何其他人像我一樣砰的一聲對抗諺語的磚牆。我永遠無法讓Package課程中的方法返回除null以外的其他任何內容。請參閱下面的修訂代碼,瞭解我如何設法實現它。

package com.example.package1; 
import java.util.*; 
import java.util.jar.*; 
import java.net.*; 

class myClass { 
    public static void main(String[] args) { 
    try { 
    new myClass(); 
    } catch (Exception e) { 
    System.out.println(e.getMessage()); 
    } finally { 
    System.out.println("Done"); 
    try{Thread.sleep(40000);}catch(Exception ee){} 
    } 
    } 

public myClass() throws Exception { 
    String clz = getClass().getSimpleName() + ".class"; 
    String pth = getClass().getResource(clz).toString(); 
    String mnf = pth.substring(0, pth.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF"; 
    String pkg = getClass().getPackage().getName().replaceAll("\\.","/"); 
    URL url = new URL(mnf); 
    Manifest manifest = new Manifest(url.openStream()); 

    Attributes attr = manifest.getAttributes(pkg); 
    String value = attr.getValue("Specification-Title") + " - " + 
    attr.getValue("Implementation-Title") + " " + 
    attr.getValue("Specification-Version") + " build # " + 
    attr.getValue("Implementation-Version"); 
    System.out.println(value); 
    } 
} 

輸出:

MyPackage - MP v1.1 build # 2015-11-05-C 
Done 

這是很多的代碼以提取四塊元數據。

所以,如果你想少一些行這裏是我代替:

public myClass() throws Exception { 
    Attributes attr = new Manifest(new URL(getClass().getResource(getClass().getSimpleName() + ".class").toString().substring(0, getClass().getResource(getClass().getSimpleName() + ".class").toString().lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF").openStream()).getAttributes(getClass().getPackage().getName().replaceAll("\\.","/")); 
    String value = attr.getValue("Specification-Title") + " - " + attr.getValue("Implementation-Title") + " " + attr.getValue("Specification-Version") + " build # " + attr.getValue("Implementation-Version"); 
    System.out.println(value); 
} 
1

添加斜線到你的包路徑結束。即將com/example/package1更改爲com/example/package1/。在包com.example.package1(我們稱之爲Foo)中請求一些類,並且一切都應該正常工作。

Package pkg = com.example.package1.class.getPackage(); 
String specVer = pkg.getSpecificationVersion(); 

尾隨斜線似乎很重要。例如。這裏是從Apache的ant.jar清單:

Name: org/apache/tools/ant/ 
Extension-name: org.apache.tools.ant 
Specification-Title: Apache Ant 
Specification-Version: 1.9.6 
Specification-Vendor: Apache Software Foundation 
Implementation-Title: org.apache.tools.ant 
Implementation-Version: 1.9.6 
Implementation-Vendor: Apache Software Foundation