3

私はjavac -sourceフラグをテストしていますが、それがどのように機能するかについて少し混乱しています。

例としてこのコードを参照してください。isEmpty()そのバージョンの JDK では String に対してメソッドが定義されていないため、互換性のない Java5 コードです。

public class TestJavac {
   public static void Main(String args[]) {
   String pippo = "pippo";   
   pippo.isEmpty();
   }
}

以下を使用してコンパイルしようとしています:

javac -source 5 TestJavac.java

それはうまくいきます!これは私には奇妙に聞こえますが、私が無視しているものがあるのか​​もしれません。への私のJAVA_HOMEポイント1.6 JDK

4

3 に答える 3

5

source オプションは、API (JDK ランタイム ライブラリによって提供される) ではなく、言語レベル専用です。

使用する JDK を指定する別のオプション (-bootclasspath -extdirs) があります (適切な jar ファイルを取得する必要があります)。

-target (出力バイトコードのバージョンを制御する) もあることに注意してください。

常にすべてを指定する必要があります(または、2番目のスイッチが機能するために多かれ少なかれ必要な古いJDKを使用してコンパイルすることもできます)

于 2012-08-03T11:00:22.287 に答える
2

-sourceソースフラグは、フラグのドキュメントから言語機能を検証するためにのみ使用されます。

1.3  The compiler does not support assertions, generics, or other
     language features introduced after Java SE 1.3.

1.4  The compiler accepts code containing assertions, which were 
     introduced in Java SE 1.4.

1.5  The compiler accepts code containing generics and other language 
     features introduced in Java SE 5.

5    Synonym for 1.5.

1.6  No language changes were introduced in Java SE 6. However, encoding 
     errors in source files are now reported as errors instead of warnings 
     as in previous releases of Java SE.

6    Synonym for 1.6.

1.7  This is the default value. The compiler accepts code with features 
     introduced in Java SE 7.

ご覧のとおり、Java6では変更は導入されていません。

于 2012-08-03T11:03:40.867 に答える
1

-source含まれているライブラリではなく、Java構文をチェックするだけです。@since 1.6たとえば、 :(を無視します

同様-targetに、古いバージョンのJVMにロードされるが、実行されない可能性のあるクラスを生成します。

http://vanillajava.blogspot.co.uk/2012/02/using-java-7-to-target-much-older-jvms.html

于 2012-08-03T11:01:43.033 に答える