➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
In this post, we will see how to read and display the .class file version? How we can find out a Java class file’s compiler version? Different 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 operating system,
> javap -verbose ClassName | findstr "major"
![Read and Display .class File Version 1 Read and display .class file version in Windows](https://www.knowprogram.com/wp-content/uploads/2020/02/Read-and-display-.class-file-version-in-Windows.png)
In Linux operating system,
$ javap -verbose ClassName | grep "major"
![Read and Display .class File Version 2 Read and display .class file version in Ubuntu](https://www.knowprogram.com/wp-content/uploads/2020/02/Read-and-display-.class-file-version-in-Ubuntu.png)
JDK Version | Major Version (in Decimal) | Major Version (in Hex) |
JDK 1.1 | 45 | 0x2D |
JDK 1.2 | 46 | 0x2E |
JDK 1.3 | 47 | 0x2F |
JDK 1.4 | 48 | 0x30 |
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 |
- By using
javap
tool you can get every information about the .class file.
javap -verbose ClassName
![Read and Display .class File Version 3 find class version using javap](https://www.knowprogram.com/wp-content/uploads/2020/02/find-class-version-using-javap.png)
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 & 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
![Read and Display .class File Version 4 find class version in linux-1](https://www.knowprogram.com/wp-content/uploads/2020/02/find-class-version-in-linux-1.png)
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.
![Read and Display .class File Version 5 find class version in linux-3](https://www.knowprogram.com/wp-content/uploads/2020/02/find-class-version-in-linux-3.png)
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
![Read and Display .class File Version 6 find class version in linux-2](https://www.knowprogram.com/wp-content/uploads/2020/02/find-class-version-in-linux-2.png)
References:-
- https://en.wikipedia.org/wiki/Java_class_file#General_layout
- https://stackoverflow.com/questions/1096148/how-to-check-the-jdk-version-used-to-compile-a-class-file
- https://reverseengineering.stackexchange.com/questions/1328/find-out-a-java-class-files-compiler-version
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!