如何在java程序中读取特定类路径jar的MANIFEST文件

时间:2013-06-17 08:14:15

标签: java jar manifest

我能够在我的java程序中读取所有类路径jar的MANIFEST文件。但我想读一个特定jar的MANIFEST文件说“xml-apis-1.0.b2.jar”怎么做?

我的工作代码是

Enumeration resEnum;
    try{
    resEnum  =Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);

    while (resEnum.hasMoreElements()) {
        try {
            URL url = (URL)resEnum.nextElement();
            InputStream is = url.openStream();

            if (is != null) {
                Manifest manifest = new Manifest(is);

                Attributes mainAttribs = manifest.getMainAttributes();
                String vendor = mainAttribs.getValue("Implementation-Vendor");
                String version = mainAttribs.getValue("Implementation-Version");
                if(!mainAttribs.getValue("Extension-Name").equals("null")){
                    System.out.println(mainAttribs.getValue("Implementation-Title"));
                                           System.out.println(version);
                    System.out.println(vendor);
                    System.out.println(mainAttribs.getValue("Extension-Name"));
                }
                }
            }catch(Exception e){}
    }
    }catch(Exception e){

2 个答案:

答案 0 :(得分:1)

您必须使用JarFile类并使用getManifest()方法获取Manifest。

答案 1 :(得分:1)

你可以试试这个

if (url.getFile().contains("xml-apis-1.0.b2.jar")) {
    ...
}