Java字节码与Java的不同版本兼容吗? [英] Is Java bytecode compatible with different versions of Java?
问题描述
如果我使用Java 5代码将应用程序编译为字节码,生成的.class文件是否可以在Java 1.4下运行?
If I compile an application using Java 5 code into bytecode, are the resulting .class files going to be able to run under Java 1.4?
如果后者可以工作并且我正在尝试在Java 1.4应用程序中使用Java 5框架,那么我应该注意什么吗?
If the latter can work and I'm trying to use a Java 5 framework in my Java 1.4 application, is there anything I should watch out for?
推荐答案
不! .class
文件仅向前兼容. Java 5引入了新的类文件属性和格式更改,以处理Varargs,枚举和泛型. Java 4处理这些代码时只会失败.
Nope! .class
files are forward compatible only. Java 5 introduced new classfile attributes and format changes, to handle Varargs, enums, and generics. Java 4 would just fail when processing these.
但是,javac上有不受支持的-target jsr14选项,它为某些Java 5语言功能生成了与JDK 1.4兼容的字节码.
However, there is the unsupported -target jsr14 option on javac that generates JDK 1.4-compatible bytecode for some Java 5 language features.
也有一些项目(例如 Retroweaver ,
Also, there are projects (e.g. Retroweaver, Retrotranslator) that convert Java 5 classfiles into Java 4 files.
我找到了这个好资源:使用Java 5语言功能在早期的JDK中
I found this good resource: Using Java 5 language features in earlier JDKs
这篇关于Java字节码与Java的不同版本兼容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!