Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this tutorial, we'll look at how to find the Java release version for a .class file. Additionally, we'll look at how to check the Java version in jar files.

2. .class Version in Java

When a Java file is compiled, a .class file is generated. In some cases, we need to find the Java release version of the compiled class file. Each Java major release assigns a major version for the .class file it generates.

In this table, we map the major version number of .class to the version of the JDK where that class version was introduced, and we show the hexadecimal representation of that version number:

Java Release Class Major Version Hex
Java SE 18 62 003e
Java SE 17 61 003d
Java SE 16 60 003c
Java SE 15 59 003b
Java SE 14 58 003a
Java SE 13 57 0039
Java SE 12 56 0038
Java SE 11 55 0037
Java SE 10 54 0036
Java SE 9 53 0035
Java SE 8 52 0034
Java SE 7 51 0033
Java SE 6 50 0032
Java SE 5 49 0031
JDK 1.4 48 0030
JDK 1.3 47 002f
JDK 1.2 46 002e
JDK 1.1 45 002d

3. javap Command for .class Version

Let's create a simple class and build it with JDK 8:

public class Sample {
    public static void main(String[] args) {
        System.out.println("Baeldung tutorials");
    }
}

In order to identify the version of the class file, we can use the Java class file disassembler javap.

Here's the syntax for the javap command:

javap [option] [classname]

Let's check the version for Sample.class as an example:

javap -verbose Sample

//stripped output ..
..
..
Compiled from "Sample.java"
public class test.Sample
  minor version: 0
  major version: 52
..
..

As we can see in the output of the javap command, the major version is 52, indicating that it's for JDK8.

While javap gives many details, we're only concerned with the major version.

For any Linux-based system, we can use the following command to obtain only the major version:

javap -verbose Sample | grep major

Similarly, for a Windows system, here's the command we can use:

javap -verbose Sample | findstr major

This gives us the major version, 52, in our example.

It's important to note that this version value doesn't indicate that the application was built with the corresponding JDK. A class file version can be different from the JDK used for compilation.

For example, if we build our code with JDK11, it should produce a .class file that has version 55. But, if we pass -target 8 during compilation, then the .class file will have version 52.

4. hexdump for .class Version

It's also possible to check the version using any hex editor. Java class file follows a specification. Let's look at its structure:

ClassFile {
    u4             magic;
    u2             minor_version;
    u2             major_version;
    // other details
}

Here, the types u1, u2, and u4 represent an unsigned one, two, and four-byte integer, respectively.
The u4 is a magic number identifying the class file format. It has the value 0xCAFEBABE, and the u2 is the major version.

For a Linux-based system, we can use the hexdump utility to parse any .class file:

> hexdump -v Sample.class
0000000 ca fe ba be 00 00 00 34 00 22 07 00 02 01 00 0b
0000010 74 65 73 74 2f 53 61 6d 70 6c 65 07 00 04 01 00
...truncated

In this example, we compiled using JDK8. The 7 and 8 indexes in the first line provide the major version of the class file. Therefore, 0034 is the hex representation, and JDK8 is the corresponding release number (from the mapping table we saw earlier).

As an alternative, we can directly get the major release version as a decimal with hexdump:

> hexdump -s 7 -n 1 -e '"%d"' Sample.class
52

Here, output 52 is the class version that corresponds to JDK8.

5. Version for jars

A jar file in the Java ecosystem consists of a collection of class files bundled together. In order to find out which Java version the jars are built or compiled, we can extract the jar file and use either javap or hexdump to check the .class file versions.

There is also a MANIFEST.MF file in the jar file, which contains some header information about the JDK used.

For example, the Build-Jdk or Created-By header stores the JDK value depending on how the jar is built:

Build-Jdk: 17.0.4

or

Created-By: 17.0.4

5. Conclusion

In this article, we learned how to find the Java version for a .class file. We saw the javap and hexdump commands and their usage for finding the version. Additionally, we looked at how to check the Java version in jar files.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments