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

如何找到编译类的目标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,
或者可能是脚本或shell
语言;)

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

我希望这有助于。

Anthony Borla

Anthony Borla

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