java - Ant 构建失败, "[javac] javac: invalid target release: 7"

标签 java ant javac

更新: See resolution here.

感谢大家的帮助!


我在尝试使用 Ant 编译项目时遇到错误,它声称“[javac] javac:无效目标版本:7”并导致构建失败。

我在 Mac OSX Mavericks 机器上运行 javac 版本 1.7.0_40。 Ant版本:2012年2月26日编译的Apache Ant(TM) version 1.8.3

只有在尝试使用 Ant 进行编译时才会出现问题。在命令行使用 javac 编译项目中的单个文件可以正常工作(使用以下命令):

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/*

build-common.xml 文件为 javac 指定:

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

相同的构建文件在其他人身上运行良好,可以在以下位置找到: https://github.com/cdlockard/appinventor-sources/blob/master/appinventor/build-common.xml

看完this ,我在我的机器上检查了早期的 Java 版本,但没有找到。

根据 this question , 我加了

executable="/usr/bin/javac"

到 build-common.xml 文件以确保它找到了正确的 Java 编译器,但错误仍然存​​在。

命令行输出如下:

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.

详细运行 ant 产生以下详细信息:

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

有什么想法吗?将不胜感激。

最佳答案

问题是 Ant 正在查看我计算机上的 Java 1.6 安装(我没有意识到它的存在),并且由于我的 $JAVA_HOME 变量没有设置,它无法找到 1.7 安装。我通过将以下行添加到我的 .bash_profile 文件来添加 $JAVA_HOME 变量:

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

问题解决,项目构建成功。

我意识到它实际上在我的 Ant 输出的最顶部通知了我 Java 版本,但我之前没有注意到它:

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

一些可能对遇到类似问题的用户有帮助的附加说明:

- According to this ,如果“fork”字段设置为 yes,Ant 将仅使用 build-common.xml 文件的 javac 部分的“executable”字段中的值。如果未列出,fork 默认为“no”,因此如果您不添加它并将其设置为“yes”,则“可执行”值将被忽略。

-在 Mac 上,可以通过/usr/bin/javac 访问正确的 javac 可执行文件。在命令行上,您可以运行

which javac

在命令行找到正在执行的文件的路径,当然还有

javac -version

找到它的版本号。如上所述,Ant 不会查看命令行 PATH 中的 javac,而是查看 JAVA_HOME。

感谢大家的帮助!

关于java - Ant 构建失败, "[javac] javac: invalid target release: 7",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21690464/

相关文章:

java - 如何使用 jdbctemplate 从 Java 执行匿名 PL/SQL

java - Java 正则表达式中撇号的位置

ant - 如何将自定义 jar 发布到本地 Apache Ivy 存储库

java - Eclipse 和 javac 之间奇怪的编译器差异

java - 访问子类对象自己的私有(private)字段

java - BigInteger.pow 和 BigInteger.isProbablePrime 的复杂度是多少?

java - Ant 脚本 : Have <exec> tag dump out entire command line

java - 科贝图拉 + 朱尼特

java - 为什么编译器声明不存在唯一的最大实例?

android - Javac 不会从 .java 创建 native .c 文件。 JNI