Java – Ant build failing, “[javac] javac: invalid target release: 7”

antjavajavac

UPDATE:
See resolution here.

Thanks to everyone for their help!


I'm encountering an error when attempting to compile a project with Ant, which is claiming "[javac] javac: invalid target release: 7" and causing the build to fail.

I am running javac version 1.7.0_40 on a Mac OSX Mavericks machine. Ant version: Apache Ant(TM) version 1.8.3 compiled on February 26 2012

It is only when attempting to compile with Ant that the problem occurs. Compiling individual files in the project with javac at the command line works properly (with the following command):

javac -d /Users/username/git/appinventor-sources/appinventor/common/build/classes/CommonUtils -classpath /Users/username/git/appinventor-sources/appinventor/common/build/classes/CommonUtils:/Users/username/git/appinventor-sources/appinventor/lib/guava/guava-14.0.1.jar -target 7 -encoding utf-8 -g:lines,vars,source -source 7 common/src/com/google/appinventor/common/utils/*

The build-common.xml file specifies for javac:

<attribute name="source" default="7"/>
<attribute name="target" default="7"/>

The same build file has worked fine for others and can be found at:
https://github.com/cdlockard/appinventor-sources/blob/master/appinventor/build-common.xml

After reading this, I checked for earlier Java versions on my machine and didn't find any.

Per this question, I added

executable="/usr/bin/javac"

to the build-common.xml file to ensure that it found the correct Java compiler, but the error continued.

The command line output is as follows:

init:

CommonUtils:
[javac] Compiling 1 source file to /Users/username/git/appinventor-sources/appinventor/common/build/classes/CommonUtils
[javac] javac: invalid target release: 7
[javac] Usage: javac <options> <source files>
[javac] use -help for a list of possible options

BUILD FAILED
/Users/username/git/appinventor-sources/appinventor/build.xml:16: The following error occurred while executing this line:
/Users/username/git/appinventor-sources/appinventor/build-common.xml:318: The following error occurred while executing this line:
/Users/username/git/appinventor-sources/appinventor/common/build.xml:42: The following error occurred while executing this line:
/Users/username/git/appinventor-sources/appinventor/build-common.xml:120: Compile failed; see the compiler error output for details.

Running ant in verbose yielded the following details:

[javac] Compiling 1 source file to /Users/username/git/appinventor-sources/appinventor/common/build/classes/CommonUtils
[javac] Using modern compiler
[javac] Compilation arguments:
[javac] '-d'
[javac] '/Users/username/git/appinventor-sources/appinventor/common/build/classes/CommonUtils'
[javac] '-classpath'
[javac] '/Users/username/git/appinventor-sources/appinventor/common/build/classes/CommonUtils:/Users/username/git/appinventor-sources/appinventor/lib/guava/guava-14.0.1.jar'
[javac] '-target'
[javac] '7'
[javac] '-encoding'
[javac] 'utf-8'
[javac] '-g:lines,vars,source'
[javac] '-verbose'
[javac] '-source'
[javac] '7'
[javac] 
[javac] The ' characters around the executable and arguments are
[javac] not part of the command.
[javac] File to be compiled:
[javac]     /Users/username/git/appinventor-sources/appinventor/common/src/com/google/appinventor/common/utils/package-info.java
[javac] javac: invalid target release: 7
[javac] Usage: javac <options> <source files>
[javac] use -help for a list of possible options
  [ant] Exiting /Users/username/git/appinventor-sources/appinventor/common/build.xml.

Any ideas? Help would be greatly appreciated.

Best Solution

The problem was that Ant was looking at a Java 1.6 install on my computer (that I hadn't realized existed), and since my $JAVA_HOME variable was not set, it was unable to find the 1.7 install. I added a $JAVA_HOME variable by adding the following line to my .bash_profile file:

export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home'

This rectified the problem, and the project built successfully.

I realized that it actually informed me of the Java version at the very top of my Ant output originally, but I hadn't previously noticed it:

Detected Java version: 1.6 in: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Some additional notes that may be helpful to users with similar issues:

-According to this, Ant will only utilize the value in the "executable" field of the javac section of the build-common.xml file if the "fork" field is set to yes. fork defaults to "no" if not listed, so if you don't add it and set it to "yes", the "executable" value will be ignored.

-On a Mac, your correct javac executable can likely be reached via /usr/bin/javac. On the command line, you can run

which javac

to find the path to the file being executed at the command line, and of course

javac -version

to find its version number. As noted above, Ant does not look at the javac in your command line PATH, but rather at JAVA_HOME.

Thanks to everyone for their help!

Related Question