The Bad Programmer

Find JDK version used to compile a class



I had a situation where I needed to know what version of the JDK was used to compile a jar file. I can’t remember the details about why I needed to know but for whatever reason I needed to know. There are two ways to find out, one is super-simple and the other is simple as well, it just requires a few more steps.

The first way if you are on a *nix machine is to try the “file” command. More recent versions of “file” will identify the JDK used to compile a class file. I can vouch for the “file” command on Mac OS, it will show you the JDK version for a class file.

  1. First get a class file out of your jar file: jar xvf YourJarFile.jar path_to_class_file_in_jar
  2. Then: file SomeClass.class

Here is some sample output:

michael:~> file Client.class
Client.class: compiled Java class data, version 51.0 (Java 1.7)

If your file command doesn’t show the JDK version or you are on Windows (try Cygwin if you use Windows). Then you can examine the class file itself. Open the class file in some sort of hex editor. On a *nix machine you can use the hexdump command. The 8th byte of the class file contains the JDK version.

For example:

michael:~> hexdump SomeOther.class | head
0000000 ca fe ba be 00 00 00 34 00 20 08 00 14 0a 00 06

In this example the 8th byte is “34” which is 52 decimal and 52 represents Java 1.8.

Here are the hex and decimal values for JDK versions:

Hex Decimal JDK Version
2E 46 Java 1.2
2F 47 Java 1.3
30 48 Java 1.4
31 49 Java 5
32 50 Java 6
33 51 Java 7
34 52 Java 8