gradlew clean build Execution failed for task compileJava

15 min read
gradlew clean build
> Task :compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> 无效的源发行版: 11

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.6.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 24s
2 actionable tasks: 1 executed, 1 up-to-date

您收到此错误是因为 Gradle 使用的 JDK 版本与 sourceCompatibility 设置不兼容。错误表明 Gradle 无法编译 Java 11 源代码。

请确保您已安装 JDK 11 或更高版本,并且 Gradle 使用正确的 JDK 版本。您可以通过以下方法更改 Gradle 使用的 JDK:

  1. gradle.properties 文件中设置:

在项目根目录下,创建或修改 gradle.properties 文件,添加以下内容:

org.gradle.java.home=<path_to_your_JDK_11_directory>

<path_to_your_JDK_11_directory> 替换为您的 JDK 11 安装目录。例如:

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
  1. 使用环境变量:

设置 JAVA_HOME 环境变量以指向 JDK 11 安装目录。在命令行中执行以下命令(对于 macOS 或 Linux):

export JAVA_HOME=<path_to_your_JDK_11_directory>

对于 Windows,请设置系统环境变量 JAVA_HOME 为 JDK 11 安装目录。

  1. 如果以上方法仍然无法解决问题,您可以尝试在项目根目录下的 gradlew(对于 Linux 和 macOS)或 gradlew.bat(对于 Windows)脚本中设置 JAVA_HOME。在脚本文件中找到以下行:
DEFAULT_JVM_OPTS='"-Xmx64m"'

在此行后面添加以下内容:

JAVA_HOME=<path_to_your_JDK_11_directory>

<path_to_your_JDK_11_directory> 替换为您的 JDK 11 安装目录。

完成更改后,再次尝试运行 gradlew clean build。如果问题仍然存在,请确保您的系统环境变量和 JDK 安装是正确的。