1. Overview
1.概述
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.
在本教程中,我们将研究如何找到.class文件的Java发布版本。此外,我们还将看看如何检查jar文件中的Java版本。
2. .class Version in Java
2.Java中的.class版本
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.
当一个Java文件被编译时,会生成一个.class文件。在某些情况下,我们需要找到编译后的类文件的Java发布版本。每个Java主要版本都会为其生成的.class文件指定一个主要版本。
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:
在这个表格中,我们将.class的主要版本号映射到引入该类版本的JDK的版本,并显示该版本号的十六进制表示。
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
3.javap命令的.class版本
Let’s create a simple class and build it with JDK 8:
让我们创建一个简单的类,并用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.
为了识别类文件的版本,我们可以使用Java类文件反汇编程序javap。。
Here’s the syntax for the javap command:
下面是javap命令的语法。
javap [option] [classname]
Let’s check the version for Sample.class as an example:
让我们以Sample.class为例检查其版本。
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.
我们可以在javap命令的输出中看到,主要版本是52,表明它是针对JDK8的。
While javap gives many details, we’re only concerned with the major version.
虽然javap给出了许多细节,但我们只关心主要版本。
For any Linux-based system, we can use the following command to obtain only the major version:
对于任何基于Linux的系统,我们可以使用下面的命令,只获得主要版本。
javap -verbose Sample | grep major
Similarly, for a Windows system, here’s the command we can use:
同样地,对于Windows系统,我们可以使用以下命令。
javap -verbose Sample | findstr major
This gives us the major version, 52, in our example.
在我们的例子中,这给了我们主要的版本,52。
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.
需要注意的是,这个版本值并不表示应用程序是用相应的JDK构建的。一个类文件的版本可能与用于编译的JDK不同。
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.
例如,如果我们用JDK11构建我们的代码,它应该产生一个.class文件,其版本为55。但是,如果我们在编译过程中传递 -target 8 ,那么 .class 文件的版本将是 52。
4. hexdump for .class Version
4.hexdump为.class版本
It’s also possible to check the version using any hex editor. Java class file follows a specification. Let’s look at its structure:
也可以用任何十六进制编辑器来检查版本。Java类文件遵循一个规范。让我们看一下它的结构。
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.
这里,类型u1、u2和u4分别代表一个无符号的一、二和四字节的整数。
u4是一个识别类文件格式的神奇数字。它的值是0xCAFEBABE,而u2是主要版本。
For a Linux-based system, we can use the hexdump utility to parse any .class file:
对于基于Linux的系统,我们可以使用hexdump工具来解析任何.class文件。
> 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).
在这个例子中,我们使用JDK8进行编译。第一行中的7和8索引提供了类文件的主要版本。因此,0034是十六进制表示,JDK8是相应的版本号(来自我们前面看到的映射表)。
As an alternative, we can directly get the major release version as a decimal with hexdump:
作为一种选择,我们可以直接用hexdump获得十进制的主要版本。
> hexdump -s 7 -n 1 -e '"%d"' Sample.class
52
Here, output 52 is the class version that corresponds to JDK8.
这里,输出52是对应于JDK8的类的版本。
5. Version for jars
5.瓶子的版本
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.
在Java生态系统中,一个jar文件由捆绑在一起的类文件的集合组成。为了找出构建或编译的Java版本,我们可以提取jar文件并使用javap或hexdump来检查.class文件版本。
There is also a MANIFEST.MF file in the jar file, which contains some header information about the JDK used.
在jar文件中还有一个MANIFEST.MF文件,它包含一些关于所使用的JDK的头信息。
For example, the Build-Jdk or Created-By header stores the JDK value depending on how the jar is built:
例如,Build-Jdk或Created-By头存储JDK值,这取决于jar的构建方式。
Build-Jdk: 17.0.4
or
或
Created-By: 17.0.4
5. Conclusion
5.结论
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.
在这篇文章中,我们学习了如何为一个.class文件查找Java版本。我们看到了javap和hexdump命令以及它们在查找版本时的用法。此外,我们还看了如何检查jar文件中的Java版本。