2

I just wanted to know if the bytecode generated in one version of java will work on other versions of java

4

2 回答 2

10

In general, bytecode will run without modification on a newer version of Java. It will not run on an older version unless you compile it with special arguments (javac -target) and take extreme care not to use the new library classes.

于 2013-03-12T01:28:58.497 回答
3

二进制兼容性

根据 JVM 规范,Java SE 7 的类文件版本是 51,因为 JSR 292 引入了调用动态字节码。Java SE 7 编译器生成的 51 版类文件不能在 Java SE 6 中使用。

Java SE 7 与 Java SE 6 二进制兼容,但不兼容。除了提到的不兼容性之外,使用 Java SE 6 编译器构建的类文件将在 Java SE 7 中正确运行。

Friends Words ...
编译器不向后兼容,因为使用 Java7 JDK 生成的字节码不会在 Java 1.6 jvm 中运行(除非使用 -target 1.6 标志编译)。但是 JVM 是向后兼容的,因为它可以运行较旧的字节码。

所以他们选择从 javac 的角度考虑兼容性(因为它是 JDK 特有的部分),这意味着生成的字节码可以在 jvm 的未来版本中运行(这与 JRE 更相关,但也捆绑在 JDK 中)。

简而言之,我们可以说:

JDK's are (usually) forward compatible.
JRE's are (usually) backward compatible.

爪哇说

交叉编译选项

默认情况下,类是针对 javac 附带的平台的引导类和扩展类编译的。但是 javac 也支持交叉编译,其中类是针对不同 Java 平台实现的引导程序和扩展类进行编译的。交叉编译时使用 -bootclasspath 和 -extdirs 很重要;请参阅下面的交叉编译示例。

-target version
    Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), and 1.7 (also 7).

    The default for -target depends on the value of -source:

        If -source is not specified, the value of -target is 1.7
        If -source is 1.2, the value of -target is 1.4
        If -source is 1.3, the value of -target is 1.4
        If -source is 1.5, the value of -target is 1.7
        If -source is 1.6, the value of -target is 1.7
        For all other values of -source, the value of -target is the value of -source.

-bootclasspath bootclasspath
    Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (:) and can be directories, JAR archives, or ZIP archives. 

有关交叉编译的更多信息,请查看 http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#crosscomp-options

在http://www.oracle.com/technetwork/java/javase/compatibility-417013.html上比我好

于 2013-03-12T01:34:00.653 回答