February 11, 2015

Determine JDK version from a *.class file

Sometimes you run into JVM class file compability issues and it's useful to know what JDK version the class file was compiled as.  Doing this is farily easy and involves using the javap command which comes bundled with the JDK. 

Suppose you have a class named org.ferris.util.CalendarTools. To determine the JDK version of this class you would do something like this:

$ cd $PROJECT_HOME/target/classes
$ javap -classpath . -verbose org.ferris.util.CalendarTools

The output from running javap will look something like this:

Compiled from "CalendarTools.java"
public class org.ferris.util.CalendarTools extends java.lang.Object
SourceFile: "CalendarTools.java"
. . .

minor version: 0
major version: 50

. . .
Constant pool:


The important part is "major version: 50".  Visit this wiki page - http://en.wikipedia.org/wiki/Java_class_file#General_layout- to translate what this number means.  You'll see that a version of 50 means "J2SE 6.0 = 50 (0x32 hex)".  So the CalendarTools class was compiled to a JDK 6 format.

Enjoy!