When using the Java compiler (javac
), we can specify two kinds of compatibility. One is using -source
and the other is using -target
. What is the difference between these two?
For example, -source 1.5
and -target 1.6
?
Also, is there any case where we use a different source and target compatibility level?
From the javac docs:
-source Specifies the version of source code accepted.
-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.
In your example:
-source 1.5 and -target 1.6
This would be used to make sure that the source code is compatible with JDK 1.5, but should generate class files for use on JDK 1.6 and later.
Quite why you would do this is another matter.
The -source
indicates what level of compliance your source code has: are you using Annotations? Then you would need at least 1.5
; are you using @override
on interface implementations, you would need 1.6
etc
The -target
specifies what Java version you want to be able to run your classes on. You could use a Java SE 7
compiler and compile to run on Java SE 1.5
.
This is mostly useful to produce a jar file working with an older version of Java.
I believe that so far all JDKs are able to execute older version too, so there is no real reason to have target larger than source.
It does however make sense to set target
to e.g. 1.6 when using a 1.7 JDK.
I'm not sure, but I believe it could work in some situations to compile a 1.7 java code using a 1.7 compiler to a 1.6 jar, for example expressions such as
ArrayList<Integer> foo = new ArrayList<>();
that are only valid in 1.7+ source version should compile to 1.6 compatible byte code. But I have not verified whether the compiler will actually do this. Unfortunately, this doesn't seem to be implemented in practise.