84

I'm using Ant 1.7.0 and installed java 1.6 which is in JAVA_HOME.

I want to build a project using java 1.5, so I've exported JAVA_HOME to be my java 1.5 directory.

java -version

says "1.5". When I run Ant it uses java 1.6.

3
  • 3
    What operating system are you using?
    – Jon Skeet
    Jun 4, 2009 at 10:20
  • 2
    You can write a batch script to specify exactly how to run it. If its just compiling, you could specify the version in the command, like: <javac target="1.5" srcdir=.../> Sep 18, 2013 at 15:54
  • Take a look this answer for wrong JAVA version
    – Harisudha
    Apr 1, 2021 at 18:08

17 Answers 17

75

Just had this issue, it happened because I'd first added the build file to the ant-view when the default JRE was 1.6.

There was no project-specific JRE and I changed the default to 1.5, even eclipse was running in 1.5, and JAVA_HOME was 1.5 too. Running the ant target from the command line used JRE 1.5, but within eclipse it still used 1.6.

I had to right-click the ant target, select Run As... and change the JRE under the JRE tab. This setting is remembered for subsequent runs.

4
  • 8
    I've just realised the question did not mention Eclipse at all. My answer is for the described problem when using Eclipse. Oct 27, 2011 at 19:45
  • 3
    Thank you for ending a few hours of frustration.
    – Dark Star1
    Jul 26, 2013 at 14:13
  • if you are not using eclipse then check out my answer at the bottom. Sep 13, 2014 at 6:02
  • I love how questions have many flavors of answers for the different environments and setup of our users.
    – Perdomoff
    Mar 28, 2018 at 18:00
34

In Eclipse:

  • Right click on your build.xml

  • click "Run As", click on "External Tool Configurations..."

  • Select tab JRE. Select the JRE you are using.

Re-run the task, it should be fine now.

0
25

According to the ant manual, setting JAVA_HOME should work - are you sure the changed setting is visible to ant?

Alternatively, you could use the JAVACMD variable.

2
  • 6
    I have ths issue myself atm and JAVA_HOME is pointing to the right JDK, however ant is still reporting the wrong java version.
    – Dark Star1
    Jul 26, 2013 at 14:11
  • JAVACMD as a full path to executable java.exe worked for me (win 10)
    – kamil.ka
    Oct 19, 2018 at 9:32
19

Run ant in verbose mode : ant -v and looks for clues.

1
  • 1
    This helped me a lot. I first tried building a project into a lib with jdk 1.8. I later discovered I needed to build it on 1.7 instead, so installed jdk 1.7, changed JAVA_HOME but still ant kept building it with 1.8. Using ant -v I discovered that ant was correctly using jdk1.7, yet the build was omitting all classes on the temporary folder because they were already there because of my previous attempt. So all I did was delete the temporary folder, and it recreated them, this time with jdk 1.7 like I wanted. Might help someone Jun 2, 2016 at 11:44
17

You can use the target and source properties on the javac tag to set a target runtime. The example below will compile any source code to target version 1.4 on any compiler that supports version 1.4 or later.

<javac compiler="classic" taskname="javac" includeAntRuntime="no" fork=" deprecation="true" target="1.4" source="1.4" srcdir="${src}" destdir="${classes}">

Note: The 'srcdir' and 'destdir' are property values set else where in the build script, e.g. <property name="classes" value="c:/classes" />

11

You could achieve that with following steps, if you are using Eclipse IDE:

  1. Right click on the task in your ant build file (build.xml).

  2. Mouse over "Run As", click on "External Tool Configurations...".

  3. Add followings to "Arguments":

    -Dant.build.javac.target=1.5 -Dant.build.javac.source=1.5
    
7

Build file:

<target name="print-version"> 
   <echo>Java/JVM version: ${ant.java.version}</echo> 
   <echo>Java/JVM detail version: ${java.version}</echo> 
</target>

Output:

[echo] Java/JVM version: 1.5
[echo] Java/JVM detail version: 1.5.0_08
6

You can also specify in a javac task what level of compatibility ( 1.4, 1.5, 1.6 ) you want to use, you can set the "source" and "target" level values, check the docs here : Javac task documentation

1
  • 1
    To guarantee compatibility, also add the -bootclasspath option when compiling. Jun 22, 2011 at 10:40
5

This is rather an old question, but I will add my notes for future references.

I had a similar issue and fixed it by changing the order of the exports in the PATH variable.

For example I was using a method of concatenating strings to my PATH by doing (this is just an example):

$> export PATH='$PATH:'$JAVA_HOME

If my variable PATH already had a java in it, the last value would be meaningless, thus the order would matter. To solve this I started inverting it by adding my variable first, then adding the PATH.

Following this idea I inverted the order that ANT_HOME was being exported. Adding JAVA_HOME before ANT_HOME.

This could be just a coincidence, but it worked for me.

1
  • This happened to me too. I wasn't sure what entry was conflicting with JAVA_HOME but when I put it at the beginning of the PATH, it worked.
    – Inversus
    Sep 24, 2012 at 22:28
3

JAVACMD is an Ant specific environment variable. Ant doc says:

JAVACMD—full path of the Java executable. Use this to invoke a different JVM than JAVA_HOME/bin/java(.exe).

So, if your java.exe full path is: C:\Program Files\Java\jdk1.8.0_211\bin\java.exe, create a new environment variable called JAVACMD and set its value to the mentioned path (including \java.exe). Note that you need to close and reopen your terminal (cmd, Powershell, etc) so the new environment variable takes effect.

2

If you are not using eclipse. Then you can change the ant java property directly on the file as mentioned here.

http://pissedoff-techie.blogspot.in/2014/09/ant-picks-up-incorrect-java-version.html

0
2

Use the following 2 properties for javac tag:

fork="yes"  
executable="full-path-to-the-javac-you-want-to-use".

Explaination of the properties can be found here

1

If you run Ant from eclipse, the eclipse will use jdk or jre that is configured in the class-path(build path).

4
  • Yes by default, but it can be configured for every ant file differently, so to check every one can be crucial... Dec 11, 2012 at 15:32
  • 8
    To find out for sure what java version ant is using:<echo message="Ant java version: ${ant.java.version}" />
    – dmolony
    Apr 4, 2013 at 10:50
  • Learning about ${ant.java.version} was a great thing for me, thanks! Apr 19, 2013 at 17:33
  • where would i put this in the build.xml ? Apr 4, 2016 at 16:23
1

Set your JAVA_HOME environment variable with the required java version (in your case java 1.5), then in build.xml use executable="${JAVA_HOME}/bin/javac" inside <javac></javac> tag .

example:

<target name="java compiler" description="Compiles the java code">
    <javac executable="${JAVA_HOME}/bin/javac" srcdir="./src" 
        destdir="${build.dir}/classes"> 
    </javac> 
</target>
0

According to the Ant Documentation, set JAVACMD environment variable to complete path to java.exe of the JRE version that you want to run Ant under.

0

By default the Ant will considered the JRE as the workspace JRE version. You need to change according to your required version by following the below.

In Eclipse:

  • Right click on your build.xml click "Run As", click on "External Tool Configurations..." Select tab JRE.

  • Select the JRE you are using.

  • Re-run the task, it should be fine now.

0

If your build file does not already have it (some do, e.g. Apache Tomcat), you can add an echoproperties target to build.xml, e.g.

<target name="echoproperties">
    <echoproperties/>
</target>

Then you can run the ant task echoproperties and look for "java.version" or ant -v and look for "Java version", e.g. on my machine

ant echoproperties | grep java.version

shows

[echoproperties] java.version=11.0.9.1

and

ant -v | grep -i "java version"

shows

Detected Java version: 11 in: /opt/java/zulu11.43.55-ca-jdk11.0.9.1-linux_x64

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.