user2215139
user2215139

Reputation: 1835

How to read MANIFEST file of a particular class path jar in java program

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

Upvotes: 2

Views: 3463

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136122

You can try this

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

Upvotes: 1

AllTooSir
AllTooSir

Reputation: 49432

You have to use the JarFile class and get the Manifest using getManifest() method.

Upvotes: 1

Related Questions