简体   繁体   中英

Unable to get version for stax2 java package

I am trying to find the stax2-api loaded in my environment. To solve the problem, I am trying to write a sample code which works fine with java.lang package, but not with org.codehaus.stax2. My question is why the sample code is not working with org.codehaus.stax2?

Working code

    import org.codehaus.stax2.*;
    public class Test {
            public static void main(String[] args) {
                    Package p = Package.getPackage("java.lang");
                    System.out.println(p.getSpecificationVersion());
                    System.out.println(p.getImplementationVersion());
            }

    }

Output:

    $ javac -cp ".:$STAX2_JAR_FILE" Test.java
    $ java -cp ".:$STAX2_JAR_FILE" Test
    1.8
    1.8.0_171

But running the same code for org.codehaus.stax2 fails

Not-working code:

    import org.codehaus.stax2.*;
    public class Test {
            public static void main(String[] args) {
                    Package p = Package.getPackage("org.codehaus.stax2");
                    System.out.println(p.getSpecificationVersion());
                    System.out.println(p.getImplementationVersion());
            }

    }

Output:

    $ javac -cp ".:$STAX2_JAR_FILE" Test.java
    $ java -cp ".:$STAX2_JAR_FILE" Test
    Exception in thread "main" java.lang.NullPointerException
            at Test.main(Test.java:5)

You cannot directly extract package manifest information of org.codehaus.stax2 which belongs to stax2-api and not a part of Java native library. So you can just read the manifest of the stax2-api.jar which should be available in your project's classpath. Here's how you can do it:

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class StaxDemo {

// This method reads manifest data from a JAR file and shows its information
public static void main(String[] args) {
    Enumeration resourcesEnum;
    try {
        resourcesEnum = Thread.currentThread().getContextClassLoader().
                        getResources(JarFile.MANIFEST_NAME);
        while (resourcesEnum.hasMoreElements()) {
            try {
                URL url = (URL) resourcesEnum.nextElement();                    
                if (url.toString().contains("stax2-api-3.0.1.jar")) {
                    InputStream inputStream = url.openStream();
                    if (inputStream != null) {
                        Manifest manifest = new Manifest(inputStream);
                        Attributes mainAttribs = manifest.getMainAttributes();
                        String specificationVersion = 
                        mainAttribs.getValue("Specification-Version");
                        String implementationVersion = 
                        mainAttribs.getValue("Implementation-Version");
                        if (specificationVersion != null && implementationVersion != 
                            null) {
                            System.out.println("Specification Version : " + 
                                                specificationVersion);
                            System.out.println("Implementation Version : " + 
                                                implementationVersion);
                        }
                    }
                }
            } catch (Exception e) {
                // handle
            }
        }
    } catch (IOException e1) {
        // handle
    }
}

} The code will produce following output:

Specification Version : 3.0.1
Implementation Version : 3.0.1

The code goes through all the JARs present in your classpath. Hence I filtered the JAR which was required. You can alter based on your requirement. Hope this helps !!!

Aman's answer work, but I found that I can use jar command to extract the manifest file and then grep for bundle-version in the manifest file. Here are the commands:

$ jar xf $STAX2_JAR_FILE META-INF/MANIFEST.MF
$ grep Bundle-Version META-INF/MANIFEST.MF
Bundle-Version: 3.1.4

I ended up finding version using the jar command on the command-line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM