2

我可以在我的 java 程序中读取所有类路径 jar 的清单文件。但我想读取特定 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){
4

2 回答 2

1

你可以试试这个

if (url.getFile().contains("xml-apis-1.0.b2.jar")) {
    ...
}
于 2013-06-17T08:29:41.040 回答
1

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

于 2013-06-17T08:16:58.400 回答