Is Java bytecode compatible with different versions of Java?

前端 未结 3 1766
粉色の甜心
粉色の甜心 2021-01-21 08:30

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?

If the latter can wo

3条回答
  •  难免孤独
    2021-01-21 09:16

    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.

    However, there is the unsupported -target jsr14 option on javac that generates JDK 1.4-compatible bytecode for some Java 5 language features.

    Also, there are projects (e.g. Retroweaver, Retrotranslator) that convert Java 5 classfiles into Java 4 files.

    EDIT: I found this good resource: Using Java 5 language features in earlier JDKs

提交回复
热议问题