Ant 使用了错误的 Java 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/949678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:27:25  来源:igfitidea点击:

Ant is using wrong java version

javaant

提问by

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

我正在使用 Ant 1.7.0 并安装了 JAVA_HOME 中的 java 1.6。

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

我想使用 java 1.5 构建一个项目,因此我已将 JAVA_HOME 导出为我的 java 1.5 目录。

java -version

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

说“1.5”。当我运行 Ant 时,它使用 java 1.6。

回答by Michael Borgwardt

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

根据ant 手册,设置 JAVA_HOME 应该可以工作 - 您确定更改的设置对 ant 可见吗?

Alternatively, you could use the JAVACMD variable.

或者,您可以使用 JAVACMD 变量。

回答by Robert Munteanu

Run ant in verbose mode : ant -vand looks for clues.

以详细模式运行 ant :ant -v并寻找线索。

回答by Billy

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

您还可以在 javac 任务中指定要使用的兼容性级别(1.4、1.5、1.6),您可以设置“源”和“目标”级别值,请查看此处的文档: Javac 任务文档

回答by simon_d

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 标记上的目标和源属性来设置目标运行时。下面的示例将在支持版本 1.4 或更高版本的任何编译器上编译任何源代码以定位版本 1.4。

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

注意:'srcdir' 和 'destdir' 是在构建脚本中的其他位置设置的属性值,例如 <property name="classes" value="c:/classes" />

回答by Sheng Chien

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

如果您使用的是 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
    
  1. 右键单击 ant 构建文件 ( build.xml) 中的任务。

  2. 将鼠标悬停在“运行方式”上,单击“外部工具配置...”。

  3. 在“参数”中添加以下内容:

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

回答by Lee Kowalkowski

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.

刚刚遇到这个问题,这是因为当默认 JRE 为 1.6 时,我首先将构建文件添加到 ant-view。

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.

没有特定于项目的 JRE,我将默认值更改为 1.5,即使 eclipse 运行在 1.5 中,JAVA_HOME 也是 1.5。从命令行运行 ant 目标使用 JRE 1.5,但在 eclipse 中它仍然使用 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.

我必须右键单击 ant 目标,选择 Run As... 并更改 JRE 选项卡下的 JRE。此设置会被记住以供后续运行使用。

回答by zawhtut

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

如果从 eclipse 运行 Ant,eclipse 将使用在类路径(构建路径)中配置的 jdk 或 jre。

回答by flavio_yama

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.

我有一个类似的问题,并通过更改 PATH 变量中的导出顺序来修复它。

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

例如,我正在使用一种将字符串连接到我的 PATH 的方法(这只是一个示例):

$> 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.

如果我的变量 PATH 中已经有一个 java,那么最后一个值将毫无意义,因此顺序很重要。为了解决这个问题,我开始通过首先添加我的变量,然后添加 PATH 来反转它。

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

按照这个想法,我颠倒了 ANT_HOME 的导出顺序。在 ANT_HOME 之前添加 JAVA_HOME。

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

这可能只是巧合,但它对我有用。

回答by bram000

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.

  • 右键单击您的 build.xml

  • 单击“运行方式”,单击“外部工具配置...”

  • 选择选项卡JRE。选择您正在使用的 JRE。

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

重新运行任务,现在应该没问题了。

回答by Utsav Gupta

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

如果你没有使用eclipse。然后,您可以直接在文件上更改 ant java 属性,如此处所述。

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

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