how to check the jdk version used to compile a .class file
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Java Development Kit (JDK) is an essential toolset for Java developers as it includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), and other tools needed in Java application development. The JDK thus determines not only how Java programs are run but also how they are created and compiled. For various reasons, such as compatibility and the use of specific Java features, it's crucial to know the version of the JDK used to compile a .class file.
Understanding Java Class Files
A .class file in Java is the compiled version of a Java source file (*.java). This file is generated by the Java compiler (javac), which translates Java code into bytecodes that the Java Virtual Machine (JVM) can understand and execute. The .class files contain metadata along with the bytecodes, which includes the JDK version used during the compilation.
Checking the JDK Version of .class Files
The JDK version used to compile a .class file is embedded within the file as part of its metadata in the form of major and minor version numbers. The process to check these versions involves reading these numbers from the binary data of the .class file.
Using the javap Tool
One convenient tool for checking the JDK version of a .class file is javap, which is a disassembler tool that comes with the JDK. The javap tool can provide the major and minor Java versions used to compile the file.
Steps:
- Opening Command Line Tool: Open your command prompt or terminal.
- Navigating to the .class File: Use the
cdcommand to navigate to the directory containing your.classfile. - Running
javapCommand: Execute the commandjavap -verbose ClassName(without the.classextension).
The output will include a line that reads major version: X and potentially minor version: Y. These indicate the version of the JDK used.
Using Hex Editors
For a more hands-on approach, you can use a hex editor to open a .class file and look directly at the bytes encoding the version information. The first four bytes of any .class file are CA FE BA BE, the magic number for Java class files. The next two bytes show the minor version, and the following two indicate the major version.
Interpreting Major Versions:
Here is what major versions correspond to various Java releases:
| Major Version | Java Version |
| 45 | 1.0 |
| 46 | 1.2 |
| 47 | 1.3 |
| 48 | 1.4 |
| 49 | 5 (1.5) |
| 50 | 6 |
| 51 | 7 |
| 52 | 8 |
| 53 | 9 |
| 54 | 10 |
| ... | ... |
Use this information to interpret the hexadecimal numbers from the hex editor.
Additional Details
Why It’s Important: Knowing the JDK version can help in understanding the features available in the compiled code. For instance, if a .class file is compiled with JDK 8, it can use features like Lambda expressions which are not available in earlier versions.
Version Specific Features: Each JDK version introduces some features but also removes or deprecates others. Ensuring compatibility especially in larger projects across different environments can save debugging and migration efforts.
Conclusion
Understanding and checking the JDK version used to compile a .class file is important for compatibility and debugging. Tools like javap and hex editors provide simple ways to inspect this metadata. This knowledge assists developers in ensuring consistent environments in development, testing, and production stages.
Related reading
- How to check two condition while using ConditionalOnProperty or ConditionalOnExpression
- How to check type of variable in Java?
- How to check whether a given string is valid JSON in Java
- How to clear gradle cache?
- How to clear the console using Java?
- How to clone an InputStream?
- How to clone ArrayList and also clone its contents?
- How to combine paths in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.