如何找到已编译类的目标 Java 版本?

2022-01-17 00:00:00 compiler-construction java

读取和显示 Java .class 版本的工具

如果我有一个已编译的 Java 类,有没有办法仅从类文件中判断它的目标版本兼容性是什么?具体来说,我有许多类文件,编译为 Java 6,它们在 Java 5 下运行并给出无法识别的版本".错误.我希望能够在不运行 JVM 的情况下查看类文件并找到其目标版本兼容性.有什么想法吗?

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?

推荐答案

我在网上找到了这个,它可以工作.

I've found this on the net and it works.

每个.class"文件都以以下:

Every '.class' file starts off with the following:

  • 幻数 [4 字节]
  • 版本信息 [4 字节]

已编译的.class"文件的十六进制转储使用以下每个选项揭示:

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

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,或者可能是脚本或外壳语言 ;) !

Perhaps you could use this information to write your own '.class' file version checking utility, using Java, or perhaps a scripting or shell language ;) !

我希望这会有所帮助.

安东尼·博拉

来自:http://bytes.com/groups/java/16603-how-determine-java-bytecode-version

相关文章