[英]Compile with current Java Runtime (7u45) and run on Runtime Version 1.6.0
我想知道是否可以使用當前的JRE 7u45開發程序,然后在具有IBM J9 VM build 2.4,JRE 1.6.0的AS400上運行它。 還是我必須使用1.6.0 JRE來開發程序? 只要我不使用稍后介紹的命令,程序就可以工作,不是嗎?
如果您對API足夠謹慎,則可以使Java 7編譯器生成與Java 6兼容的字節碼,但這不是默認行為。
我必須補充一點,這似乎不是明智的舉動。 API會以微妙的方式以及顯着的方式進行更改,並且在Java編譯器上設置“合規級別”參數不會阻止您調用JDK 6中不存在的任何方法。
有兩個問題在起作用:
1)您不能使用任何新的Java 7功能並嘗試生成1.6字節碼。 使用-source 1.6 -target 1.6
可以解決此問題。 如果這樣做,它將嘔吐(例如,使用菱形運算符; List<String> foo = new ArrayList<>()
)
2)您不能使用新的類或在1.7中添加到類的新方法。 它將正常編譯,然后在運行時在1.6 JVM上失敗。 這是一個棘手的問題。 使用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>
我每天都這樣做。
絕對確定代碼可以正常運行而不會意外使用不存在的功能的最好方法是使用Java 6進行開發。 對於正在安裝Java 6 JDK並將其添加到Eclipse JRE首選項的eclipse。
請注意,內置文件支持不支持CCSID。 請改用jt400.jar中的IFSFile。
另外,如果使用JAVA或RUNJVA調用程序,則可以使用SystemDefaults.properties提供系統屬性和JVM參數。 檢查信息中心。
您應該使用JDK
而不是JRE
。
另外,請使用1.6.0
而不是1.7
因為1.7版中的某些功能是1.6版不支持的 。
在編譯程序時,您需要使用“ -target”標志並將其設置為1.6
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.