Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant is using the wrong Java Compiler but thinks it's right

Just to be clear, this is not this question: 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

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.

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:

[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?

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

<?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: 
like image 856
emish Avatar asked Apr 16 '13 05:04

emish


1 Answers

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.

In your actual Ant script, why not echo the following in the build target:

<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.

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 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.

like image 55
ingyhere Avatar answered Sep 21 '22 16:09

ingyhere