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
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"
In Linux, $ javap -verbose ClassName | grep "major"
|JDK Version||Major Version |
|Major Version |
|Java SE 5.0||49||0x31|
|Java SE 6.0||50||0x32|
|Java SE 7.0||51||0x33|
|Java SE 8.0||52||0x34|
|Java SE 9.0||53||0x35|
|Java SE 10.0||54||0x36|
|Java SE 11.0||55||0x37|
|Java SE 12.0||56||0x38|
|Java SE 13.0||57||0x39|
|Java SE 14.0||58||0x3A|
javap tool you can get every information about the .class file.
javap -verbose ClassName
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
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.
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