【问题标题】:Ant build failing, "[javac] javac: invalid target release: 7"Ant 构建失败,“[javac] javac:无效的目标版本:7”
【发布时间】:2014-03-08 13:36:14
【问题描述】:

更新: See resolution here.

感谢大家的帮助!


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

我在 Mac OSX Mavericks 机器上运行 javac 版本 1.7.0_40。 Ant 版本:2012 年 2 月 26 日编译的 Apache Ant(TM) 版本 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.

有什么想法吗?非常感谢您的帮助。

【问题讨论】:

  • 你不是说1.7吗? (相对于7)。
  • 其实7是合法的。我怀疑问题出在ant 使用 java6 jdk
  • 2.7 出来的时候会很有趣。 50 年后。
  • 问题是 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' 这解决了问题。感谢大家的帮助!
  • @clock,很高兴你知道了。我看你是新来的。请为未来的用户回答您自己的问题。你知道为什么它在命令行中有效,但在 ant 中无效吗?

标签: java ant javac


【解决方案1】:

问题是 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”,则“executable”值将被忽略。

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

which javac

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

javac -version

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

感谢大家的帮助!

【讨论】:

    【解决方案2】:

    我的解决方案是更改所有 project.properties 文件

    javac.source=1.8
    javac.target=1.8
    

    javac.source=1.7
    javac.target=1.7
    

    【讨论】:

    • 我遇到了同样的问题。这也是唯一的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2010-12-02
    • 2013-09-08
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    相关资源
    最近更新 更多