Many versions of JDK: How do I specify which one is used?

落花浮王杯 提交于 2020-01-24 08:46:08

问题


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?


回答1:


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 use String.isEmpty() which appeared in Java 6. This might lead to errors at runtime



回答2:


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




回答3:


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"/>


来源:https://stackoverflow.com/questions/7071827/many-versions-of-jdk-how-do-i-specify-which-one-is-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!