【问题标题】:How to read meta data from Manifest file如何从 Manifest 文件中读取元数据
【发布时间】:2016-02-06 19:40:15
【问题描述】:

我正在尝试从我的清单文件中提取信息以显示在我的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 对象的定义正确,但它没有读取任何规范或实现属性。

【问题讨论】:

  • 我认为这个问题回答了你的问题:stackoverflow.com/questions/1272648/…
  • @Clayton,谢谢,但我已经去过那里并尝试过。我在那里尝试了几种选择,但没有比这里更进一步;包名,但属性只有 null 值。
  • 这些从包中检索属性的方法应该可以工作,否则它们有什么用途?我是否只是针对 jar 中的 manifest.mf 文件对它们的功能进行了过多的解释?我觉得我一定错过了一些简单的东西。这让我发疯了。

标签: java manifest.mf


【解决方案1】:

所以我终于想通了,我想我会分享一下,以防其他人像我一样用头撞在众所周知的砖墙上。我永远无法让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);
}

【讨论】:

    【解决方案2】:

    在包路径的末尾添加一个斜杠。 IE。将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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-14
      • 2020-02-26
      • 1970-01-01
      • 2015-12-10
      • 2012-06-10
      • 2016-04-24
      • 2021-08-18
      • 2012-06-27
      相关资源
      最近更新 更多