1

I have Java 6 and 7 installed. How do I specify ant to use Java 6 to compile? I've seen some threads that set the java 6 compiler path, and that worked, but I also saw threads like this Setting the target version of Java in ant javac which set a simple attribute in the javac task. And then sometimes it says to specify the target and the source.

I read here http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javac.html#examples but still do not understand the use of the two attributes. From the oracle docs, it seems like source specifies the language rules (e.g. "this uses java6 rules, so there won't be any try-with-resource blocks"), and target specifies the java-version-bytecode to generate? The docs seem to imply the source should always be set, but how come the question in the first link just says just to specify the target=1.6 attribute?

Community
  • 1
  • 1
Raekye
  • 5,081
  • 8
  • 49
  • 74

1 Answers1

3

ANT is itself a Java program, so it uses the JAVA_HOME environment variable to choose which JVM to use at runtime.

Some ANT tasks allow you to choose a different Java compiler or JRE, over-riding the default option. For more details see the ANT documentation:

Update

I have Java 6 and 7 installed. How do I specify ant to use Java 6 to compile?

The sources and target attributes of the javac task control compatibility settings. they do not control which JDK is used by ANT.

They enable a modern JDK to compile older versions of the Java programming language and/or produce bytecode compatible with older versions of the Java runtime.

sources is part of the standard options and targets is detailed under the cross compilation section of the javac command's documentation from Oracle:

Finally, Oracle have produced the following document on Java compatibility:

It states that Java 6 and Java 7 are strongly compatible, which was not always the case in previous versions of Java.

PS Apologies to repeating the same links, but hopefully the concepts are clearer?

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • Yes, I'm more or less aware of that. As in my second link (the orale javac docs), it talks about "target" and "source". But I'm not sure what the use of each one is. What happens if I specify source but not target? Target but not source? – Raekye May 11 '13 at 19:04
  • Thanks, that link was useful. But to confirm, `source` specifies the language definition/syntax, and `target` specifies what bytecode to generate? – Raekye May 13 '13 at 03:36
  • Thanks. Is it possible to compile with 1.7 features (e.g. try-with-catch blocks) to 1.6 code? By specfiying `source=1.7` and target=`1.6`? – Raekye May 13 '13 at 14:51
  • @Raekye That is what it says on the tin! :-) – Mark O'Connor May 13 '13 at 15:45