Many versions of JDK: How do I specify which one is used?
I have installed many versions of the JDK: 1.4.2, 1.5 a开发者_开发技巧nd 1.6
.
How do I specify which version of the JDK is used when compiling using Ant?
Two solutions:
- Specify the full path in your command:
for example
/opt/java/jdk16/bin/javac ...
on Linux - Use the
-source
and-target
arguments of the javac command. This allows you specify the source code level and targeted JRE version
Also note:
- Some Linux distributions can include tools to specify which JDK version to use by default.
- Using
-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 useString.isEmpty()
which appeared in Java 6. This might lead to errors at runtime
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
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"/>
精彩评论