2013-03-12 18 views

回答

10

的其他版本的做工一般,字節碼將運行而無需在修改更新版本版本的Java。除非您使用特殊參數(javac -target)編譯它,並且非常小心不要使用新的庫類,否則它將不會運行在較舊的版本上。

+4

你還需要使用' -source'以便使用'-target'。在某些情況下,在早期版本上編譯的源代碼會選擇不同的重載方法,因此「極度小心」並不完全取決於它。 Netter使用'-bootclasspath'指向早期的'rt.zip',而對於擴展庫則有'-Dext.dirs'。 – 2013-03-12 01:45:06

+1

但是,真的,在這一點上,最好只安裝並行的JDK ... – 2013-03-12 06:45:35

3

二進制兼容性

Java SE 7的類文件的版本是51,按照JVM規範,因爲由JSR引入由Java SE產生292.版51的類文件的invokedynamic字節碼7編譯器不能用於Java SE 6.

Java SE 7與Java SE 6二進制兼容,但incompatibilities除外。除注意不兼容,與Java SE 6編譯器生成的類文件將在Java SE正常運行7.

朋友詞...
編譯器是不向後兼容,因爲與Java7 JDK生成的字節碼贏」運行在Java 1.6 jvm中(除非使用-target 1.6標誌編譯)。但JVM向後兼容,因爲它可以運行較舊的字節碼。

因此,他們選擇從javac的角度考慮兼容性(因爲它是JDK特有的部分),這意味着所生成的字節碼可以在未來的jvm版本中運行(更關係到JRE,但也捆綁在JDK中)。

總之,我們可以說:

JDK's are (usually) forward compatible. 
JRE's are (usually) backward compatible. 

的Java說

交叉編譯選項

默認情況下,類是對引導和延伸編譯javac附帶的平臺的類。但是javac也支持交叉編譯,其中類是針對不同Java平臺實現的引導程序和擴展類編譯的。交叉編譯時使用-bootclasspath和-extdirs是很重要的;請參閱下面的交叉編譯示例。

-target version 
    Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), and 1.7 (also 7). 

    The default for -target depends on the value of -source: 

     If -source is not specified, the value of -target is 1.7 
     If -source is 1.2, the value of -target is 1.4 
     If -source is 1.3, the value of -target is 1.4 
     If -source is 1.5, the value of -target is 1.7 
     If -source is 1.6, the value of -target is 1.7 
     For all other values of -source, the value of -target is the value of -source. 

-bootclasspath bootclasspath 
    Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (:) and can be directories, JAR archives, or ZIP archives. 

更多關於交叉編譯看看 http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#crosscomp-options

比我更好的 http://www.oracle.com/technetwork/java/javase/compatibility-417013.html