我正在学习如何使用 Gradle 在 IntelliJ 中构建项目。我想构建一个涉及 OpenJFX 的项目。但是,我遇到了这个错误。
java.lang.UnsupportedClassVersionError: org/openjfx/gradle/JavaFXPlugin has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
> org/openjfx/gradle/JavaFXPlugin has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
据我所知,Gradle 使用 Java 8 来运行 Java 11 插件。但是,我把它放在
gradle.properties
:org.gradle.java.home=/C:/Users/<my user>/.jdks/openjdk-14.0.1
它在 .jdks 中的原因是因为 IntelliJ 较早下载了它。问题是,我告诉 Gradle 使用 Java 14,但它给了我一个错误,好像它是由 Java 8 运行的。这里有什么问题?
最佳答案
根据 the docs org.gradle.java.home
属性(property)…
…specifies the Java home for the Gradle build process. The value can be set to either a
jdk
orjre
location, however, depending on what your build does, using a JDK is safer. A reasonable default is derived from your environment (JAVA_HOME
or the path tojava
) if the setting is unspecified. This does not affect the version of Java used to launch the Gradle client VM
因此,此值仅用于构建过程(如编译或运行应用程序或测试)。 Gradle 会为此启动一个子进程,因此执行任务的 VM 和运行构建脚本的 VM 可能不同。
看起来您使用的是用 Java 11 (55.0) 编译的 Gradle 插件,但您的 Gradle VM 是 Java 8 (52.0),因此它无法加载和使用插件的类。您必须将 Java 11+ 用于 Gradle VM 本身。
尝试像
JAVA_HOME=C:/Users/<my user>/.jdks/openjdk-14.0.1 gradlew.bat clean run
一样运行您的构建.
关于java - 当我告诉它运行 Java 14 时,为什么 Gradle 会尝试使用 Java 8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62204545/