Ant使用了错误的Java编译器,但认为它';是的

Ant使用了错误的Java编译器,但认为它';是的,java,ant,environment-variables,javac,Java,Ant,Environment Variables,Javac,要明确的是,这不是一个问题: 我正试图在MacOS10.8.2机器上使用Ant编译一个Java1.7项目。我已经安装了Java1.7,Eclipse已经顺利地构建了它。但是,当使用ant构建脚本并执行ant命令行时 ant -v 输出充其量也很神秘:第一行显示Ant正在按照请求使用Java1.7,在我自己设置的$JavaU HOME位置 Apache Ant(TM) version 1.8.2 compiled on December 20 2010 Trying the default bu

要明确的是,这不是一个问题:

我正试图在MacOS10.8.2机器上使用Ant编译一个Java1.7项目。我已经安装了Java1.7,Eclipse已经顺利地构建了它。但是,当使用ant构建脚本并执行ant命令行时

ant -v
输出充其量也很神秘:第一行显示Ant正在按照请求使用Java1.7,在我自己设置的$JavaU 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
但是,以下编译器错误毫无意义,因为它们揭示了仅在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]                             ^
编辑:根据评论员的要求,我在下面添加了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>
试一试

您将看到Ant认为Java安装在哪里。它可能是从Eclipse中使用的工具之外的其他地方获得的,因为Eclipse允许用户在每个项目的基础上指定JDK工具的位置。您还可以运行
ant-diagnostics
来查看完整的诊断输出,它基本上会添加Java版本信息等信息

在实际的Ant脚本中,为什么不在构建目标中回显以下内容:

<echo message="Ant running on Java version ${ant.java.version}"/>
<javac debug="true" 
... 
source="1.7" 
target="1.7" 
...
executable="<MY_JDK7_JAVA_HOME_DIR>/bin/javac" 
... >
... 
</javac>

另外,通过指定“-v”选项,可以在详细模式下运行Ant,ala
Ant-v…
。这些信息将指导调试问题,并有望解决问题

请向我们展示manifest.mf和build.xml文件的一些代码,尝试在调用
build.xml
中的
javac
任务时设置源代码级别的版本。是否导入了必要的包?@Jesper setting source level仅接受版本1到5,而不接受版本6或7。我查看了manjavac和-source标志。@NandakishoreK所有正确的包都被导入了。编译错误是Java 7中不应该出现的语法错误。
ant -diagnostics | grep java\\.home
<echo message="Ant running on Java version ${ant.java.version}"/>
<javac debug="true" 
... 
source="1.7" 
target="1.7" 
...
executable="<MY_JDK7_JAVA_HOME_DIR>/bin/javac" 
... >
... 
</javac>