Reputation: 199
I am testing the javac -source
flag and I am a bit confuse about how it is supposed to work.
See this code as an example. It is a non-compatible Java5 code as the method isEmpty()
is not defined for String in that version of the JDK.
public class TestJavac {
public static void Main(String args[]) {
String pippo = "pippo";
pippo.isEmpty();
}
}
Trying to compile using:
javac -source 5 TestJavac.java
Then it works!
This sounds weird to me, but maybe there is something that I am ignoring.
My JAVA_HOME
point to a 1.6 JDK
.
Upvotes: 3
Views: 3843
Reputation: 29927
The source flag is used only to verify language features, from the documentation of -source
flag:
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.
as you can see, no changes were introduced in java 6.
Upvotes: 2
Reputation: 533530
The -source
only checks the java syntax, not the libraries included. e.g. it ignores the @since 1.6
:(
Similarly -target
will produce classes which will load on an older version of the JVM but might not run.
http://vanillajava.blogspot.co.uk/2012/02/using-java-7-to-target-much-older-jvms.html
Upvotes: 1
Reputation: 262534
The source option is only for the language level, not for the API (supplied by the JDK runtime libraries).
There is a separate option (-bootclasspath -extdirs) to specify which JDK to use (and you need to get the appropriate jar files).
Note that there is also -target (which controls the output bytecode version).
You should always specify all (or even compile using the older JDK, which you more or less need anyway for that second switch to work)
Upvotes: 5