如何找到编译类的目标 Java 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/698129/
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 can I find the target Java version for a compiled class?
提问by Mike Pone
Duplicate:
复制:
If I have a compiled Java class, is there a way to tell from just the class file what its target version compatibility is? Specifically, I have a number of class files, compiled to Java 6, which are running under Java 5 and giving the the "Unrecognized version" error. I want to be able to look at a class file and find what its target version compatibility is without running the JVM. Any ideas?
如果我有一个已编译的 Java 类,有没有办法仅从类文件中判断其目标版本兼容性是什么?具体来说,我有许多类文件,编译为 Java 6,它们在 Java 5 下运行并给出“无法识别的版本”错误。我希望能够在不运行 JVM 的情况下查看类文件并找到其目标版本兼容性。有任何想法吗?
采纳答案by Kalecser
I've found this on the net and it works.
我在网上找到了这个,并且有效。
Every '.class' file starts off with the following:
- Magic Number [4 bytes]
- Version Information [4 bytes]
A hexdump of a '.class' file compiled with each of the following options reveals:
javac -target 1.1
==>CA FE BA BE 00 03 00 2D
javac -target 1.2
==>CA FE BA BE 00 00 00 2E
javac -target 1.3
==>CA FE BA BE 00 00 00 2F
javac -target 1.4
==>CA FE BA BE 00 00 00 30
Perhaps you could use this information to write your own '.class' file version checking utility, using Java, or perhaps a scripting or shell language ;) !
I hope this helps.
Anthony Borla
每个“.class”文件都以以下内容开头:
- 幻数 [4 字节]
- 版本信息 [4 字节]
使用以下每个选项编译的“.class”文件的十六进制转储显示:
javac -target 1.1
==>CA FE BA BE 00 03 00 2D
javac -target 1.2
==>CA FE BA BE 00 00 00 2E
javac -target 1.3
==>CA FE BA BE 00 00 00 2F
javac -target 1.4
==>CA FE BA BE 00 00 00 30
也许您可以使用这些信息来编写自己的“.class”文件版本检查实用程序,使用 Java,或者脚本或 shell 语言;)!
我希望这有帮助。
安东尼·博拉
From: http://bytes.com/groups/java/16603-how-determine-java-bytecode-version
来自:http: //bytes.com/groups/java/16603-how-determine-java-bytecode-version
回答by Bj?rn
You can look at the byte offset 6 and 7 in the file (in a hex dump probably), which tells you which version is used. I think the Bytecode Visualizer(eclipse plugin) can see which version a class file is made for.
您可以查看文件中的字节偏移量 6 和 7(可能在十六进制转储中),它告诉您使用的是哪个版本。我认为字节码可视化器(eclipse 插件)可以看到类文件是为哪个版本制作的。
回答by bruno conde
You can use the javaputility that comes with the standard JDK.
您可以使用标准 JDK 附带的javap实用程序。
javap -verbose MyClass
Compiled from “MyClass.java”
public class MyClass extends java.lang.Object
SourceFile: “MyClass.java”
minor version: 3
major version: 45
回答by Kevin Crowell
Taken from: http://twit88.com/blog/2008/09/22/java-check-class-version/
摘自:http: //twit88.com/blog/2008/09/22/java-check-class-version/
try {
String filename = "C:\MyClass.class";
DataInputStream in = new DataInputStream(new FileInputStream(filename));
int magic = in.readInt();
if (magic != 0xcafebabe) {
log.info(filename + " is not a valid class!");
}
int minor = in.readUnsignedShort();
int major = in.readUnsignedShort();
log.info(filename + ": " + major + " . " + minor);
in.close();
} catch (IOException e) {
log.info("Exception: " + e.getMessage(), e);
}
回答by user2240219
Linux/Unix users have a nice tool out of the standard toolbox: file
utility. Modern versions can detect the Java class fersion version and even output Java version for known class file types.
Linux/Unix 用户拥有标准工具箱之外的一个很好的工具:file
实用程序。现代版本可以检测 Java 类转换版本,甚至可以输出已知类文件类型的 Java 版本。
Example output:
示例输出:
com/sample/Tracker.class: compiled Java class data, version 45.3
com/sample/TestListener.class: compiled Java class data, version 49.0 (Java 1.5)
And it fits very nicely into the standard Unix scripting toolchain.
它非常适合标准的 Unix 脚本工具链。