Read and display .class file version

Read and display .class file version | Find out a Java class file’s compiler version | Tool to read and display Java .class versions | check which JDK version compiled the class?

After compilation of a .java file, compiler generates .class file. Every .class file contains minor_version & major_version attributes that represent the version of the .class file. The JVM (Java virtual machine) uses minor_version & major_version attributes to identify the version of the compiler which generates the given .class file.

There are different ways to read and display the .class file version name.

We can use javap tool for this purpose.

In Windows, 
> javap -verbose ClassName | findstr "major" 
Read and display .class file version in Windows
Windows
In Linux,
$ javap -verbose ClassName | grep "major"
Read and display .class file version in Ubuntu
Ubuntu
JDK VersionMajor Version
(in Decimal)
Major Version
(in Hex)
JDK 1.1450x2D
JDK 1.2460x2E
JDK 1.3470x2F
JDK 1.4480x30
Java SE 5.0490x31
Java SE 6.0500x32
Java SE 7.0510x33
Java SE 8.0520x34
Java SE 9.0530x35
Java SE 10.0540x36
Java SE 11.0550x37
Java SE 12.0560x38
Java SE 13.0570x39
Java SE 14.0580x3A

By using javap tool you can get every information about the .class file.

javap -verbose ClassName
find class version using javap

The .class file generated by the lower version of the compiler can be executed by the higher version JVM, but the .class file generated by the higher version of the compiler can’t be executed by the lower version JVM.

For example:- If a .class is generated by Java SE 8.0 can be executed by Java SE 13.0, but the .class file generated by the Java SE 13.0 can’t be executed by the Java SE 8.0 version compiler.


Other ways to read and display .class file version In Linux

Go to the directory where the .class file is located and execute the command in terminal

$ file ClassName.class

It Will give the file type and version as well. Here is what the output looks like:
ClassName.class: compiled Java class data, version 57.0

find class version in linux-1

You can also read and display .class file major_version without going to the directory.

$ file path-to-file/ClassName.class

In my case, the java file “Hello.java” is located in the “Java” directory. So, the below command gives the version number.

find class version in linux-3

Using hex editor

If you don’t have access to javap, you can also use a console-based utility or GUI hex editor to look at the value of the byte at position 7 in the file. Using Linux, this is easily done using the ‘od’ dump standard utility.

$ od --format=d1 MyClass.class -j 7 -N 1

The output looks like

0000007   57
0000010
find class version in linux-2

References:-

Leave a Comment

Your email address will not be published. Required fields are marked *