Reputation:
I have installed many versions of the JDK: 1.4.2, 1.5 and 1.6
.
How do I specify which version of the JDK is used when compiling using Ant?
Upvotes: 3
Views: 1056
Reputation: 1573
Use the Ant <javac>
task<source>
and/or <target>
attributes. Valid values can be from 1.1 to 1.7, with 5, 6, and 7 valid aliases for 1.5, 1.6 and 1.7. Also, the <executable>
attribute can be used to set which java javac compiler is used. For example:
<javac source="1.4" target="1.4" executable="c:\java1.6\bin\javac.exe"/>
Upvotes: 1
Reputation: 20875
Two solutions:
/opt/java/jdk16/bin/javac ...
on Linux-source
and -target
arguments of the javac command. This allows you specify the source code level and targeted JRE versionAlso note:
-source
and -target
checkes that your language constructs are compliant with the targeted runtime, but does NOT check that core classes are compatible. This means that compiling with -source 1.4
on a JDK 1.6 will be just fine, even if you use String.isEmpty()
which appeared in Java 6. This might lead to errors at runtimeUpvotes: 5
Reputation: 24910
javac -source 1.4 -target 1.4 YourFile.java
-source release Specifies the version of source code accepted. The following values for release are allowed: 1.3 the compiler does not support assertions, generics, or other language features introduced after JDK 1.3. 1.4 the compiler accepts code containing assertions, which were introduced in JDK 1.4. 1.5 the compiler accepts code containing generics and other language features introduced in JDK 5. The compiler defaults to the version 5 behavior if the -source flag is not used. 5 Synonym for 1.5
Here is the relevant documentation.
http://download.oracle.com/javase/1,5.0/docs/tooldocs/windows/javac.html
Upvotes: 1