Ant is using wrong java version

前端 未结 16 1386
小蘑菇
小蘑菇 2020-11-28 09:20

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.

相关标签:
16条回答
  • 2020-11-28 10:00

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

    0 讨论(0)
  • 2020-11-28 10:00

    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.

    0 讨论(0)
  • 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 your terminal (cmd, Powershell, etc) so the new environment variable takes effect.

    0 讨论(0)
  • 2020-11-28 10:01

    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
    
    0 讨论(0)
  • 2020-11-28 10:05

    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.

    0 讨论(0)
  • 2020-11-28 10:08

    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 讨论(0)
提交回复
热议问题