java 如何以编程方式确定当前类的Java字节码版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1707139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine the Java byte code version of the current class programatically?
提问by Thorbj?rn Ravn Andersen
I have a situation where the deployment platform is Java 5 and the development happens with Eclipse under Java 6 where we have established a procedure of having a new workspace created when beginning work on a given project. One of the required steps is therefore setting the compiler level to Java 5, which is frequently forgotten.
我有一种情况,部署平台是 Java 5,开发是在 Java 6 下使用 Eclipse 进行的,我们已经建立了在开始处理给定项目时创建新工作区的过程。因此,必需的步骤之一是将编译器级别设置为 Java 5,这经常被遗忘。
We have a test machine running the deployment platform where we can run the code we build and do initial testing on our PC's, but if we forget to switch the compiler level the program cannot run. We have a build server for creating what goes to the customer, which works well, but this is for development where the build server is not needed and would add unnecessary waits.
我们有一台运行部署平台的测试机,我们可以在其中运行我们构建的代码并在我们的 PC 上进行初始测试,但是如果我们忘记切换编译器级别,程序将无法运行。我们有一个构建服务器,用于创建给客户的东西,效果很好,但这是用于不需要构建服务器的开发,并且会增加不必要的等待。
The question is: CANI programmatically determine the byte code version of the current class, so my code can print out a warning already while testing on my local PC?
现在的问题是: CAN我编程方式确定当前类的字节代码版本,所以我的代码已经可以打印出一个警告,而我的本地PC上进行测试?
EDIT: Please note the requirement was for the current class. Is this available through the classloadeer? Or must I locate the class file for the current class, and then investigate that?
编辑:请注意,要求是针对当前课程的。这可以通过类加载器获得吗?或者我必须找到当前类的类文件,然后进行调查?
采纳答案by Shimi Bandiel
Take a look at question: Java API to find out the JDK version a class file is compiled for?
回答by mm101
Easy way to find this to run javapon class
找到这个在类上运行javap 的简单方法
For more details goto http://download.oracle.com/javase/1,5.0/docs/tooldocs/windows/javap.html
有关更多详细信息,请访问http://download.oracle.com/javase/1,5.0/docs/tooldocs/windows/javap.html
Example:
例子:
M:\Projects\Project-1\ant\javap -classpath M:\Projects\Project-1\build\WEB-INF\classes -verbose com.company.action.BaseAction
and look for following lines
并寻找以下几行
minor version: 0
major version: 50
回答by McDowell
You could load the class file as a resourceand parse the first eight bytes.
//TODO: error handling, stream closing, etc.
InputStream in = getClass().getClassLoader().getResourceAsStream(
getClass().getName().replace('.', '/') + ".class");
DataInputStream data = new DataInputStream(in);
int magic = data.readInt();
if (magic != 0xCAFEBABE) {
throw new IOException("Invalid Java class");
}
int minor = 0xFFFF & data.readShort();
int major = 0xFFFF & data.readShort();
System.out.println(major + "." + minor);
回答by dweeves
here is the Java Class File Format descriptor: Java Class File Format
这是 Java 类文件格式描述符: Java 类文件格式
and here the major version values:
这里是主要版本值:
public static final int JDK14_MAJOR_VERSION = 48;
公共静态最终 int JDK14_MAJOR_VERSION = 48;
public static final int JDK15_MAJOR_VERSION = 49;
公共静态最终 int JDK15_MAJOR_VERSION = 49;
public static final int JDK16_MAJOR_VERSION = 50;
公共静态最终 int JDK16_MAJOR_VERSION = 50;
Now, read the class file with Java code and check the major version to know which JVM generated it
现在,读取带有 Java 代码的类文件并检查主要版本以了解哪个 JVM 生成它
回答by stepanian
Bytes 5 through 8 of a class file content is the version number in hex. You can use Java code (or any other language) to parse the version number.
类文件内容的第 5 到第 8 字节是十六进制版本号。您可以使用 Java 代码(或任何其他语言)来解析版本号。

