Find out what version a Java Class is compiled with

Writing by shivdev on Tuesday, 17 of February , 2009 at 6:12 pm

I got an UnsatisfiedLinkError and wanted to find out whether our JDK upgrade had caused it and did some searching and found that javap -verbose lets you find out the version of a compiled class. So get the class (could be in a JAR file) and then follow this example where I find the version of MyCallback.class

C:>c:\jdk1.6.0_07\bin\javap -verbose MyCallback
Compiled from “MyCallback.java”
public class com.shivdev.MyCallback extends java.lang.Object implements com.shivdev.IFilter
SourceFile: “MyCallback.java”
minor version: 0
major version: 49

Then compare with the following:

major  minor  JDK
45       3    1.0
45       3    1.1
46       0    1.2
47       0    1.3
48       0    1.4
49       0    1.5
50       0    1.6

Alternatively, you can use this java class to find out the version:

public class FindVersion {

    private static int convert(byte upper, byte lower) {
        int ret =  upper << 8;
        ret |= lower;
        return ret;
    }

    public static void main(String[] args) throws Exception {
    		final String CLASS = "MyCallback.class";
        ClassLoader loader = FindVersion.class.getClassLoader();
        java.io.InputStream in = loader.getResourceAsStream(CLASS);
        try {
            byte[] buffer = new byte[8];
            in.read(buffer);
            int minor = convert(buffer[4], buffer[5]);
            int major = convert(buffer[6], buffer[7]);
            System.out.println(major + "." + minor);
        } finally {
            in.close();
        }
    }
}

Leave a comment

Category: Java

Shivdev Kalambi's Blog

Shivdev Kalambi is a Principal Software Engineer working at ArcSight. He has over 11 years' experience in software development and has worked with several technologies and played different roles and contributed to all phases of projects. Other non-tech activies include Ping-pong, Racquetball, Rock Climbing at PG and Swimming.