Ant 使用了错误的 Java 编译器,但认为它是正确的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16029668/
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
Ant is using the wrong Java Compiler but thinks it's right
提问by emish
Just to be clear, this is not this question: Ant is using wrong java version
需要明确的是,这不是这个问题:Ant is using wrong java version
I'm trying to compile a Java 1.7 project using Ant on a Mac OS 10.8.2 machine. I've got Java 1.7 installed, and Eclipse has been building it swimmingly. However, when using an ant build script with command line execution of Ant like so
我正在尝试在 Mac OS 10.8.2 机器上使用 Ant 编译 Java 1.7 项目。我已经安装了 Java 1.7,并且 Eclipse 一直在顺利地构建它。但是,当使用 ant 构建脚本和 Ant 的命令行执行时
ant -v
the output is mysterious at best: The first lines reveal that Ant is using Java 1.7 as requested, from the location of $JAVA_HOME, as I set up myself.
输出充其量是神秘的:第一行显示 Ant 正在按照要求使用 Java 1.7,从 $JAVA_HOME 的位置,我自己设置。
Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Trying the default build file: build.xml
Buildfile: /Users/emish/School/cis555/homework/cis-555-search-engine/build.xml
Detected Java version: 1.7 in: /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre
Detected OS: Mac OS X
However, the following compiler errors make no sense, because they reveal a compiler error that would only occur in a version prior to Java 1.7:
但是,以下编译器错误没有任何意义,因为它们揭示了仅在 Java 1.7 之前的版本中才会发生的编译器错误:
[javac] /Users/emish/School/cis555/homework/cis-555-search-engine/src/pokedex/crawler/CrawlCore.java:186: <identifier> expected
[javac] } catch (IOException | ClassNotFoundException | InterruptedException e) {
[javac] ^
This is very weird. I've tried many things, including re-installing my Java JDK and reinstalling Ant. Does anyone out there have some insight into what might be causing this puzzling problem?
这很奇怪。我尝试了很多事情,包括重新安装我的 Java JDK 和重新安装 Ant。有没有人对可能导致这个令人费解的问题的原因有所了解?
If it's any help:
如果有任何帮助:
java -version
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
javac -version
javac 1.7.0_17
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home
ll `which javac`
lrwxr-xr-x 1 root wheel 75 Oct 30 22:27 /usr/bin/javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac
ll `which java`
lrwxr-xr-x 1 root wheel 74 Oct 30 22:27 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
Edit: As requested by commenters, I've added my build.xml and manifest.mf files below.
编辑:根据评论者的要求,我在下面添加了我的 build.xml 和 manifest.mf 文件。
build.xml
构建文件
<?xml version="1.0"?>
<project name="pokedex" default="jar" basedir=".">
<property name="shared.root" location="${basedir}"/>
<property name="project.root" location="${basedir}"/>
<property name="build.dir" location="${project.root}${file.separator}build"/>
<property name="src" location="${project.root}${file.separator}src"/>
<property name="build.classes" location="${build.dir}${file.separator}classes"/>
<property name="test.classes" location="${build.classes}${file.separator}test"/>
<property name="test.source" location="${src}${file.separator}test"/>
<property name="lib.dir" location="${project.root}${file.separator}lib"/>
<target name="jar" depends="clobber, compile" description="Create Jar file">
<jar destfile="pokedex.jar">
<fileset dir="${build.classes}" includes="**/*.class"/>
<fileset dir="${project.root}" includes="conf/*"/>
</jar>
</target>
<target name="compile" depends="clobber" description="compiles Java source code">
<javac srcdir="${src}${file.separator}" destdir="${build.classes}" debug="on" deprecation="off"
optimize="on" includeAntRuntime="no">
<classpath>
<fileset dir="${lib.dir}">
<include name="*.jar"/>
<include name="*.zip"/>
</fileset>
</classpath>
</javac>
</target>
<target name="pack" depends="jar" description="Create an archive use on EC2">
<zip destfile="pokedex.zip">
<zipfileset dir="." excludes="target/**,extra/**,**/*.class,pokedex.zip"/>
</zip>
</target>
<target name="clobber" description="remove jar">
<delete file="${project.root}${file.separator}pokedex.jar"/>
<delete dir="${build.classes}${file.separator}"/>
<mkdir dir="${build.classes}${file.separator}"/>
</target>
<!--DOES NOT WORK -->
<target name="test" description="Run tests">
<java failonerror="true" fork="true" classname="junit.textui.TestRunner">
<classpath>
<pathelement location="${test.classes}"/>
<pathelement location="${build.classes.dir}"/>
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</classpath>
<arg value="RunAllTests"/>
</java>
</target>
<target name="test2">
<junit>
<classpath>
<pathelement location="${build.classes.dir}"/>
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</classpath>
<batchtest>
<fileset dir="${project.root}">
<include name="**/RunAllTests*" />
</fileset>
</batchtest>
<formatter type="brief" usefile="false"/>
</junit>
</target>
</project>
manifest.mf
清单文件
Manifest-Version: 1.0
Class-Path:
回答by ingyhere
Try
尝试
ant -diagnostics | grep java\.home
You will see where Ant thinks Java is installed. It's possible that it is getting it from somewhere besides what you are using in Eclipse because, of course, Eclipse lets the user specify on a per project basis where the JDK tooling is located. You can also just run ant -diagnostics
to see the full diagnostic output which will basically add information like Java version information, etc.
您将看到 Ant 认为 Java 的安装位置。除了您在 Eclipse 中使用的内容之外,它可能从其他地方获取它,因为当然,Eclipse 允许用户在每个项目的基础上指定 JDK 工具所在的位置。您也可以运行ant -diagnostics
以查看完整的诊断输出,它基本上会添加 Java 版本信息等信息。
In your actual Ant script, why not echo the following in the build target:
在您的实际 Ant 脚本中,为什么不在构建目标中回显以下内容:
<echo message="Ant running on Java version ${ant.java.version}"/>
This will provide information to differentiate the container where Ant is running versus the one you want your application to compile into.
这将提供信息来区分运行 Ant 的容器与您希望应用程序编译到的容器。
You can force your application to compile with a specific Java version by adding these specifiers to the javac
specifier in the build target:
您可以通过将这些说明符添加到javac
构建目标中的说明符来强制您的应用程序使用特定的 Java 版本进行编译:
<javac debug="true"
...
source="1.7"
target="1.7"
...
executable="<MY_JDK7_JAVA_HOME_DIR>/bin/javac"
... >
...
</javac>
Also, you can run Ant in verbose mode by specifying the '-v' option, ala ant -v ...
. This information should guide debugging the problem and hopefully solving it.
此外,您可以通过指定 '-v' 选项 ala 以详细模式运行 Ant ant -v ...
。此信息应指导调试问题并有望解决问题。