The Problem
In some cases, when use ant to compile code, we may have to use a specific JDK version which may be different than the default jdk in the system: for example, the project may use some jdk internal code which is not good, but sometimes we have to deal with the legacy project.
Solution 1: Using JAVACMD Environment Variable
We can define a system environment variable: JAVACMD which points to full path of the Java executable. Ant will use the JVM defined by JAVACMD instead of JAVA_HOME/bin/java
set "JAVACMD=C:\Program Files\Java\jdk1.6.0_45\bin\java.exe"
export "JAVACMD=/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/bin/java"
But this will make all ant project uses same JVM defined by JAVACMD.
Solution 2: Using javac executable and fork
We can define specify system environment to specific JDK, for example JAVA6_HOME to point to JDK6.
Then in the ant build.xml, define a property env.java.home which point to JAVA6_HOME, then in javac task, set fork=yes and sepecify executable to ${env.java.home}/bin/javac
Complete path to the javac executable to use in case of fork="yes". Defaults to the compiler of the Java version that is currently running Ant. Ignored if fork="no".
Resources
Running Apache Ant
Ant javac
In some cases, when use ant to compile code, we may have to use a specific JDK version which may be different than the default jdk in the system: for example, the project may use some jdk internal code which is not good, but sometimes we have to deal with the legacy project.
Solution 1: Using JAVACMD Environment Variable
We can define a system environment variable: JAVACMD which points to full path of the Java executable. Ant will use the JVM defined by JAVACMD instead of JAVA_HOME/bin/java
set "JAVACMD=C:\Program Files\Java\jdk1.6.0_45\bin\java.exe"
export "JAVACMD=/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/bin/java"
Solution 2: Using javac executable and fork
We can define specify system environment to specific JDK, for example JAVA6_HOME to point to JDK6.
Then in the ant build.xml, define a property env.java.home which point to JAVA6_HOME, then in javac task, set fork=yes and sepecify executable to ${env.java.home}/bin/javac
<property environment="env"/> <property name="env.java.home" value="${env.JAVA6_HOME}"/> <javac destdir="${dest-path}/main" fork="yes" executable="${env.java.home}/bin/javac" source="1.6" target="1.6" debug="on" encoding="utf8" classpathref="common.classpath"> <src path="./src/main" /> </javac>javac executable
Complete path to the javac executable to use in case of fork="yes". Defaults to the compiler of the Java version that is currently running Ant. Ignored if fork="no".
Resources
Running Apache Ant
Ant javac