需要明确的是,这不是这个问题:Ant is using wrong java version
我正在尝试在 Mac OS 10.8.2 机器上使用 Ant 编译 Java 1.7 项目。我已经安装了 Java 1.7,并且 Eclipse 一直在流畅地构建它。但是,当使用带有 Ant 命令行执行的 Ant 构建脚本时,像这样
ant -v
输出充其量是神秘的:第一行显示 Ant 正在按照我自己设置的 $JAVA_HOME 的位置使用 Java 1.7。
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
但是,以下编译器错误毫无意义,因为它们揭示了仅在 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] ^
这很奇怪。我已经尝试了很多事情,包括重新安装我的 Java JDK 和重新安装 Ant。有没有人对可能导致这个令人费解的问题的原因有所了解?
如果有任何帮助:
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
编辑:根据评论者的要求,我在下面添加了我的 build.xml 和 manifest.mf 文件。
构建.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-Version: 1.0
Class-Path: