JDK7交叉编译

要解决的问题

用JDK7编译出能在JRE6里头运行的class文件

Maven Compiler Plugin

(Maven Compiler Plugin)[http://maven.apache.org/plugins/maven-compiler-plugin/]

Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler's boot classpath to match the target JRE or use the Animal Sniffer Maven Plugin to verify your code doesn't use unintended APIs

来源: http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

仅仅去设定target的值并不能保证你的程序可以在更低版本的jre上面运行,因为这个只是language level上面的保证。

解决思路

  1. 用RT.JAR去验证

    <compilerArguments>
    <bootclasspath>xxxxxxxxx</bootclasspath>
    </compilerArguments>
    

    这个还能叫做交叉编译,用其他的JRE去验证

  2. 直接用别的版本的JAVAC去编译
    examples/compile-using-different-jdk.html

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <verbose>true</verbose>
        <fork>true</fork>
        <executable>${jdk6.home}/bin/javac</executable>
        <compilerVersion>1.6</compilerVersion>
    </configuration>
</plugin>

问题是这就不是交叉编译了。。。

想要检查最后出来的字节文件是什么版本的java字节码

javap -verbose jp.co.worksap.updater.api.ServerType | grep major

你可能感兴趣的:(JDK7交叉编译)