gpt4 book ai didi

java - Ant 没有使用正确的 Java 版本

转载 作者:行者123 更新时间:2023-11-29 09:05:56 25 4
gpt4 key购买 nike

我在安装了 Java jdk(和 jre)1.6 的 Unix 机器上通过 Eclipse Juno 生成了一个 ant 文件。当在 eclipse 之外运行 ant 时,build.xml 文件所在的机器上,一切顺利。

现在,问题是当我尝试在另一台机器上运行 ant 时,其中同时安装了 jdk 和 jre 1.6。这是我在另一台运行 RedHat 3 的机器上得到的(无法更改):

> ant -version
Apache Ant version 1.5.2-23 compiled on November 12 2003

> java -version
java version "1.6.0_39"
Java(TM) SE Runtime Environment (build 1.6.0_39-b04)
Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode, sharing)

> javac -version
javac 1.6.0_39

> ls -l `which java`
lrwxrwxrwx 1 root root 26 Feb 13 15:22 /usr/bin/java -> /usr/java/default/bin/java*

> ls -l `which javac`
lrwxrwxrwx 1 root root 27 Feb 13 15:22 /usr/bin/javac -> /usr/java/default/bin/javac*

> ls -l /usr/java
total 8
lrwxrwxrwx 1 root root 16 Nov 7 14:19 default -> /usr/java/latest
drwxr-xr-x 7 root root 4096 Feb 13 15:02 jdk1.6.0_39/
drwxr-xr-x 7 root root 4096 Nov 7 14:18 jre1.6.0_37/
lrwxrwxrwx 1 root root 21 Feb 13 15:03 latest -> /usr/java/jdk1.6.0_39

> echo $JAVA_HOME
/usr/java/jdk1.6.0_39/bin/java

当我在 build.xml 文件中添加这一行时,我得到以下结果

build.xml:
<echo message="Java version: ${ant.java.version}"/>

> ant
[echo] Java version: 1.4

和编译错误:

  [javac] ...InputManager.java:11: error: Invalid method declaration, method name required.
[javac] Map<Double, List<MyObject>> loadFile(File pSelectedFile)
[javac] ^
[javac] ...InputManager.java:11: error: Class or interface declaration expected.
[javac] Map<Double, List<MyObject>> loadFile(File pSelectedFile)
[javac] ^
[javac] ...InputManager.java:33: error: Invalid character '@' in input.
[javac] @Override
[javac] ^
...

现在,问题是:我的 java 安装不正确吗?我对 Ant 做错了吗?

这是 build.xml 文件的快速摘录:

> vim build.xml

<project basedir="." default="build" name="myProject">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="/opt/eclipse"/> <!-- that is also where eclipse is on my 2nd machine -->
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
[some stuff]
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target depends="init" name="build-project">
<echo message="Java version: ${ant.java.version}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="myProject.classpath"/>
</javac>
</target>
</project>

欢迎帮助:)

最佳答案

您的 ant 版本是针对旧版本的 java (2003) 编译的。 Java 1.6 于 2006 年问世。据我所知,您可以让 ant 在旧版本上运行,如下所示:http://www.coderanch.com/t/108814/tools/jdk-version-build-xml :

...............

If Ant has been compiled against 1.5 then you can modify the build task to

<javac source="1.4" target="1.4" ...> ...................... But not the other way around. So I'm guessing you need a fresher version of Ant.

来自 http://ant.apache.org/faq.html#java-version

Redhat ES 3.0 comes installed with ant 1.5.2. Even if you have your PATH and ANT_HOME variables set correctly to a later version of ant, you will always be forced to use the preinstalled version.

To use a later version of ant on this OS you could do the following:

$ ant -version
Apache Ant version 1.5.2-23 compiled on November 12 2003
$ su -
# rpm -e ant ant-libs
# exit
$ hash -r
$ ant -version
Apache Ant version 1.6.2 compiled on July 16 2004

关于java - Ant 没有使用正确的 Java 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15064183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com