简体   繁体   English

使用当前的Java运行时(7u45)进行编译并在运行时版本1.6.0上运行

[英]Compile with current Java Runtime (7u45) and run on Runtime Version 1.6.0

I'd like to know if I can develop my program with the current JRE 7u45 and later run it on an AS400 with IBM J9 VM build 2.4, JRE 1.6.0. 我想知道是否可以使用当前的JRE 7u45开发程序,然后在具有IBM J9 VM build 2.4,JRE 1.6.0的AS400上运行它。 Or will I have to use the 1.6.0 JRE to develop my program? 还是我必须使用1.6.0 JRE来开发程序? As long as I don't use commands that were introduced later the program should work, shouldn't it? 只要我不使用稍后介绍的命令,程序就可以工作,不是吗?

If you are careful enough with the APIs, you can make the Java 7 compiler produce Java 6-compliant bytecode, but it is not the default behavior. 如果您对API足够谨慎,则可以使Java 7编译器生成与Java 6兼容的字节码,但这不是默认行为。

I must add that this doesn't seem like a wise way to move; 我必须补充一点,这似乎不是明智的举动。 the APIs change in subtle as well as prominent ways and setting the "compliance level" parameters on the Java compiler will not prevent you from calling any methods which do not exist in JDK 6. API会以微妙的方式以及显着的方式进行更改,并且在Java编译器上设置“合规级别”参数不会阻止您调用JDK 6中不存在的任何方法。

There are two issues at play: 有两个问题在起作用:

1) You can't use any new Java 7 features and attempt to generate 1.6 bytecode. 1)您不能使用任何新的Java 7功能并尝试生成1.6字节码。 Compiling with -source 1.6 -target 1.6 solves this problem. 使用-source 1.6 -target 1.6可以解决此问题。 It will puke if you've done so (eg used the diamond operator; List<String> foo = new ArrayList<>() ) 如果这样做,它将呕吐(例如,使用菱形运算符; List<String> foo = new ArrayList<>()

2) You can't use new classes or new methods added to classes in 1.7. 2)您不能使用新的类或在1.7中添加到类的新方法。 It will compile fine, then fail at runtime on the 1.6 JVM. 它将正常编译,然后在运行时在1.6 JVM上失败。 This is a trickier problem. 这是一个棘手的问题。 Using maven to build your project plus the animal-sniffer plugin solves this issue: 使用maven来构建您的项目以及animal-sniffer插件可以解决此问题:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <compilerArgument>-Xlint:all</compilerArgument>
    </configuration>
</plugin>
<plugin>
    <!-- ensure that only methods available in java 1.6 can
         be used even when compiling with java 1.7+ -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>animal-sniffer-maven-plugin</artifactId>
    <version>1.9</version>
    <configuration>
        <signature>
            <groupId>org.codehaus.mojo.signature</groupId>
            <artifactId>java16</artifactId>
            <version>1.1</version>
        </signature>
    </configuration>
    <executions>
        <execution>
            <phase>test</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

I do this daily. 我每天都这样做。

The best way to be absolutely certain that the code will work as written without accidentally using features not present is to use java 6 for development. 绝对确定代码可以正常运行而不会意外使用不存在的功能的最好方法是使用Java 6进行开发。 For eclipse that is installing a java 6 JDK and adding it to the Eclipse JRE preferences. 对于正在安装Java 6 JDK并将其添加到Eclipse JRE首选项的eclipse。

Note that the built in File support does not support CCSID's. 请注意,内置文件支持不支持CCSID。 Use IFSFile in jt400.jar instead. 请改用jt400.jar中的IFSFile。

Also, if you use JAVA or RUNJVA to invoke your program, you can use SystemDefaults.properties to provide system properties and JVM arguments. 另外,如果使用JAVA或RUNJVA调用程序,则可以使用SystemDefaults.properties提供系统属性和JVM参数。 Check infocenter. 检查信息中心。

You should use JDK instead of JRE . 您应该使用JDK而不是JRE

Also, use 1.6.0 instead of 1.7 because there are some features in version 1.7 that version 1.6 doesn't support . 另外,请使用1.6.0而不是1.7因为1.7版中的某些功能是1.6版不支持的

在编译程序时,您需要使用“ -target”标志并将其设置为1.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM