Compiling Java 7 to Java 6

后端 未结 4 2025
轻奢々
轻奢々 2020-12-05 07:06

I\'m aware that the runtime features of Java 7 are not available with Java 6 but since no new byte code has been added the new byte code invokedynamic is

4条回答
  •  天涯浪人
    2020-12-05 07:52

    Mark a .class file output by Java 7 javac with version 1.6.0 (i.e. 0x32)

    printf "\x00\x00\x00\x32" |dd of=Example.class seek=4 bs=1 count=4 conv=notrunc
    

    (according to http://en.wikipedia.org/wiki/Java_class_file#General_layout)

    If you put that (using $1 for the filename) into j6patch you can do all class files with:

    find . -name \*.class |xargs -I {} ./j6patch {}
    

    I used this on a large (~4.8 MB jar) code base and even used RetroTranslator on the java 6 jar so Java 7 language features can be used on an app that runs in Java 5. Also the Java 7 compiler (javac) does lots of extra optimizations (e.g. escape analysis) that very noticeably improves performance.

    Using RetroTranslator with -verify -target 1.5 and JRE 1.6 runtime jars allows to verify that no Java 7 runtime features are used.

提交回复
热议问题