How to check the JDK version of a class file

  • 17 March 2020
  • ADM

 

How to check the JDK version of a class file - images/logos/java.jpg

 

When running a Java application it is mandatory that the JRE version to match or to be higher that the JDK version used to compile the application.

java.lang.unsupportedclassversionerror HelloWorld unsupported major.minor version 52.0

You will get the "Unsupported major.minor version 52.0" error when trying to run an Java 1.8 application using a lower version of Java.

In this article we will present how to check the JDK version used to compile a class in two options.

The Java version is presented into the class file as "major version" using two digits number. Here are some example values of the major version corresponding to the Java version:

  • Java 1.2 uses major version 46
  • Java 1.3 uses major version 47
  • Java 1.4 uses major version 48
  • Java 5 uses major version 49
  • Java 6 uses major version 50
  • Java 7 uses major version 51
  • Java 8 uses major version 52
  • Java 9 uses major version 53
  • Java 10 uses major version 54
  • Java 11 uses major version 55

Example

For this article we will take a simple hello world example:

package com.admfactory;

public class HelloWorld {

	public static void main(String[] args) {
		System.out.println("Hello world!!");
	}
}

Option 1

If you have the JDK installed and the JDK/bin folder it is in the PATH system environment variable, to get the Java version a class was compiled with, run the command:

Linux
javap -verbose HelloWorld.class | grep "major"
Windows
javap -verbose HelloWorld.class | findstr "major"

The command will search for the major version in the result, using command grep for Linux and findstr for Windows

Output

D:\Workspace\admfactory\bin\com\admfactory
λ javap -verbose HelloWorld.class | grep "major"
  major version: 52

Option 2

If you don't have the JDK installed it might be easier to use an hex editor (or viewer) like Frhed or Total Commander to open the class file and check the Java version used to compile the class.

The bytes 7 and 8 contain the Java version.

How to check the JDK version of a class file - /images/javaVersion001.jpg

In this case 00 34 -> Java 8 = 52 (0x34 hex).

 

References