How to read the MANIFEST file from a particular class jar in the java program

advertisements

I am able to read MANIFEST file of all my class path jars in my java program. But i want to read MANIFEST file of a particular jar say "xml-apis-1.0.b2.jar" How it can be done?

My working code is

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){


You can try this

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