Ant uses the wrong Java compiler, but considers correct

To be clear, this is not a question: Ant is using the wrong version of java

I am trying to compile a Java 1.7 project using Ant on a Mac OS 10.8.2 machine. I have Java 1.7 installed and Eclipse builds it smoothly. However, when using Ant build script with Ant command line execution like this

ant -v 

the conclusion is mysterious at best: the first lines show that Ant uses Java 1.7 on request, from the location of $ JAVA_HOME, as I configured myself.

 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 do not make sense, since they detect a compiler error that will occur only in versions prior to 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] ^ 

It is very strange. I have tried many things, including reinstalling the Java JDK and reinstalling Ant. Does anyone know what might cause this mysterious problem?

If this helps:

 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: at the request of commentators, I added the build.xml and manifest.mf files below.

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: 
+7
source share
1 answer

Try

 ant -diagnostics | grep java\\.home 

You will see where Ant thinks Java is installed. It is possible that he gets it from somewhere besides what you use in Eclipse, because, of course, Eclipse allows the user to specify, based on each project, where the JDK toolkit is located. You can also simply run ant -diagnostics to see the full diagnostic output, which will basically add information such as Java version information, etc.

In your actual Ant script, why not repeat the following for the build purpose:

 <echo message="Ant running on Java version ${ant.java.version}"/> 

This will provide information to distinguish the container in which Ant is running, compared to the one you want to compile.

You can force the application to compile with a specific version of Java by adding these qualifiers to the javac specifier in the build target:

 <javac debug="true" ... source="1.7" target="1.7" ... executable="<MY_JDK7_JAVA_HOME_DIR>/bin/javac" ... > ... </javac> 

Alternatively, you can run Ant in verbose mode by specifying the option '-v', ala ant -v ... This information should help debug the problem and hopefully solve it.

+6
source

All Articles