Find out what version a Java Class is compiled with

Writing by 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 Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.