11

I need to compile my source code to be compatible with jre 1.6. However, when I attempt to set the compiler attribute of the javac task to be javac1.6, ant will still compile my code with javac1.7. I have also tried setting the compiler version to be "modern" and that did not help.

<target name="compile-tests">
    <javac compiler="javac1.6" includeantruntime="false" srcdir="${test.dir}"
     destdir="${build.dir}" >
        <classpath refid="class.path" />
    </javac>
</target>

My JAVA_HOME is set to JDK 1.6:

echo $JAVA_HOME </code> gives: <code>
/usr/lib/jvm/java-6-openjdk-amd64/

My ant version is: Apache Ant(TM) version 1.8.2

According to this post, ant uses its own compiler. How do I override the ant default? Also, according to this post and the ant documentation, I can set the global build.compiler property. What do I set that property to be and how might I do that?

Neowizard
  • 2,795
  • 1
  • 18
  • 39
user847352
  • 123
  • 1
  • 1
  • 5

5 Answers5

39

In the javac task, try specifying the Java compiler by setting the executable attribute as follows:

<property name="JDK1.6.dir" location="/usr/lib/jvm/java-6-openjdk-amd64" />
<property name="javac1.6" location="${JDK1.6.dir}/bin/javac" />

<target name="compile-tests">
  <javac executable="${javac1.6}" 
      fork="yes"
      includeantruntime="false" 
      srcdir="${test.dir}"
      destdir="${build.dir}" >
    <classpath refid="class.path" />
  </javac>
</target>
Christopher Peisert
  • 15,875
  • 3
  • 54
  • 78
4

I think Ant compiler attribute is just to know which properties are supported by the compiler. In javac in general the attribute or option to pass to the compiler is target.

You should not even need to have a Java 6 compiler installed.

madth3
  • 7,001
  • 11
  • 45
  • 69
4

I thought it would be nice to add an updated answer to this question, since I recently dealt with a lot of headaches surrounding this same issue. My specific use case: My computer uses JRE1.8, but I needed ant to target JRE1.7 running on the server. The trick for me was to use the following attributes in combination (referencing the documentation):

  • srcdir: Location of the java files.
  • destdir: Location to store the class files.
  • includeantruntime: Whether to include the Ant run-time libraries in the classpath. It is usually best to set this to false so the script's behavior is not sensitive to the environment in which it is run.
  • source: Java language features accepted by compiler
  • target: Generate class files for specific JVM version (cross-compile).
  • fork: Whether to execute javac using the JDK compiler externally.
  • compiler: The compiler implementation to use.

You don't have to specify the executable file unless you have really specific dependencies (e.g. more recent enterprise version as opposed to latest free version).

Example:

<javac srcdir="${source}"
       destdir="${java.output.dir}"
       includeantruntime="false"
       source="1.7"
       target="1.7"
       fork="yes"
       compiler="javac1.7">
</javac>
1

Use the "target" attribute of the Ant javac task to define the version of Java that you want your class files to be compatible with (so set it to "1.6" in your case). See the task documentation for more details.

gareth_bowles
  • 19,908
  • 5
  • 52
  • 79
0

Talking about the basics of Ant, since it requires a JVM to run, it is suffice for it to recognize a JRE for this purpose. However if the build process needs to compile classes then it will be dependent on a Java compiler and if it doesn't find one then the build will fail.

It is possible to set up Ant to use one of several Java compilers, however I will focus on Oracle's javac for the explanation below. Here are the alternatives ...

1) If the JRE recognized by Ant for its execution is a sub directory under the JDK, it will raise upto the JDK level and identify the required resources.

2) If JAVA_HOME environment variable is set pointing to a JVM (not JRE) then ant will be able to compile Java artifacts

3) Alternatively, as per the Ant documentation, JAVACMD can be set in a batch file specific to the OS (antrc_pre.bat for Windows) placed under your home directory (and not that of Ant) then it will recognize the JVM. Please note JAVACMD should refer to the full path of Java.exe under a bin folder and not of JVM home.

4) Alternatively the Ant command line can take the list of libraries using -l options. Pass a tools.jar to -l option and it would suffice.

5) Finally fork attribute of JavaC task can be set as explained above. Keep in mind this is resource intensive and not recommended.