Ant 构建失败,“[javac] javac:目标版本无效:7”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21690464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:05:14  来源:igfitidea点击:

Ant build failing, "[javac] javac: invalid target release: 7"

javaantjavac

提问by clock

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.

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

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

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

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):

只有在尝试使用 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/*

The build-common.xml file specifies for javac:

build-common.xml 文件为 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

相同的构建文件对其他人运行良好,可以在以下位置找到: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.

读完这篇之后,我在我的机器上检查了早期的 Java 版本,但没有找到。

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.

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

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:

详细运行 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.

Any ideas? Help would be greatly appreciated.

有任何想法吗?帮助将不胜感激。

采纳答案by clock

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:

问题是 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'

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:

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

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.

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

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

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

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.

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

Thanks to everyone for their help!

感谢大家的帮助!

回答by mr.wolle

My solutions was to change in all project.propertiesfile

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

javac.source=1.8
javac.target=1.8

to

javac.source=1.7
javac.target=1.7