2011-12-16 40 views
0

我是「java世界」中的新成員,因此我需要了解有關不同OS和JDK中可移植性的基礎知識。我在互聯網上看到的是,如果在JDK 1.7上編譯程序,它將在任何JDK版本> = 1.7上運行,是不是?謝謝,我對我的英語表示歉意,這不是我的默認語言。Java可移植性不同OS/JDK

+0

「粗略」。當你這樣做時,你會遇到(相對較少的)不一致。我記得最常見的問題是使用「平臺默認字符集」的「新字符串(byte [])」(以及一些類似的)。只是爲了它。當你遇到一個問題......那麼,這會產生更多有趣的問題。 – 2011-12-16 05:32:07

回答

1

現在沒有在java -target版本選項
它說,...

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). 

     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. 

所以「如果在JDK 1.7的編譯程序,它可以在任何版本的JDK運行> = 1.7,是對的「

+0

其中還需要指定`-source`和`-bootclasspath`選項。幸運的是,Java 7 SDK最終警告`source` /`target`沒有`bootclasspath`。 – 2011-12-16 05:43:04

0

你是對的。你寫的大多數東西都會自動工作在任何(版本兼容的)JVM中。我將一個重要的Java Swing GUI應用程序從Windows移到Linux,我只需要將配置文件更改爲指向不同的目錄。它只是工作。

0

是的,除非在新版本中棄用某些東西。棄用可能會導致一些不一致。