[英]IntelliJ - Invalid source release: 17
I've created a new Java project in IntelliJ with Gradle that uses Java 17. When running my app it has the error Cause: error: invalid source release: 17
.我在 IntelliJ 中使用 Gradle 创建了一个新的 Java 项目,该项目使用 Java 17。运行我的应用程序时出现错误Cause: error: invalid source release: 17
。
My Settings我的设置
I've installed openjdk-17
through IntelliJ
and set it as my Project SDK
.我已经通过IntelliJ
安装了openjdk-17
并将其设置为我的Project SDK
。
The Project language level
has been set to 17 - Sealed types, always-strict floating-point semantics
. Project language level
已设置为17 - Sealed types, always-strict floating-point semantics
。
In Modules -> Sources
I've set the Language level
to Project default (17 - Sealed types, always strict floating-point semantics)
.在Modules -> Sources
中,我将Language level
设置为Project default (17 - Sealed types, always strict floating-point semantics)
。
In Modules -> Dependencies
I've set the Module SDK
to Project SDK openjdk-17
.在Modules -> Dependencies
中,我将Module SDK
设置为Project SDK openjdk-17
。
In Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler
I've set the Project bytecode version
to 17
.在Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler
我已将Project bytecode version
设置为17
。
Gradle Gradle
plugins {
id 'org.springframework.boot' version '2.5.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'com.fasterxml.jackson.core:jackson-core:2.13.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0'
}
test {
useJUnitPlatform()
}
I've looked at all of the answers here but I can't seem to fix this.我已经在这里查看了所有答案,但我似乎无法解决这个问题。 I must be missing something but I can't find it.我一定错过了什么,但我找不到。 I've not had any problems using Java 8 or 11.我使用 Java 8 或 11 没有任何问题。
How do I resolve this?我该如何解决这个问题?
In intellij just set Gradle JVM to Java version 17.在 intellij 中,只需将 Gradle JVM 设置为 Java 版本 17。
"File -> Settings.. -> Build, Execution, Deployment -> Build Tools -> Gradle" there select your project and set Gradle JVM to your java 17.0.1 “文件->设置..->构建,执行,部署->构建工具-> Gradle”选择您的项目并将Gradle JVM设置为您的java 17.0.1
The message typically entails that your JAVA_HOME environment variable points to a different Java version.该消息通常表明您的JAVA_HOME环境变量指向不同的Java版本。
Here are the steps to follow:以下是要遵循的步骤:
echo $JAVA_HOME
*nix 系统: echo $JAVA_HOME
echo %JAVA_HOME%
Windows 系统: echo %JAVA_HOME%
export JAVA_HOME=/path/to/openjdk-17
*nix 系统: export JAVA_HOME=/path/to/openjdk-17
set JAVA_HOME=path\to\openjdk-17
Windows系统: set JAVA_HOME=path\to\openjdk-17
sourceCompatibility
)确保设置源和目标兼容性版本(不仅是sourceCompatibility
)You should be able to build your project.您应该能够构建您的项目。
You may need also to instruct Gradle to use a different JVM than the one it uses itself by setting the Java plugin toolchain to your target version:您可能还需要通过将Java插件工具链设置为您的目标版本来指示Gradle使用与其自身使用的不同的JVM :
// build.gradle
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
Set JAVA_ HOME TO JDK 17 and check this by Opening cmd -> javac.将 JAVA_HOME 设置为 JDK 17 并通过打开 cmd -> javac 进行检查。 This should return the current version of java set in your machine这应该返回您机器中设置的当前 java 版本
There could be many reasons due to which this error is thrown by intellij intellij 引发此错误的原因可能有很多
echo $JAVA_HOME
if JAVA_HOME is not correct, update the JAVA_HOME environment variable.如果 JAVA_HOME 不正确,请更新 JAVA_HOME 环境变量。
File > Project Structure > Project Setting > Project -> SDK and Language level should match
File > Project Structure > Project Setting > Modules -> Language Level should match with SDK
Preferences > Build, Execution and Deployment > Gradle > Gradle JVM should match with SDK (Preferences > Build, Execution and Deployment > maven if you're using maven)
Preferences > Build, Execution and Deployment > Compiler > Java Compiler > Project Bytecode version (this should match with your SDK as well)
If you start your project from a main method directly invoked from Intelij then the missing dialog execution configurations may be the cause of the error.如果您从直接从 Intelij 调用的 main 方法启动项目,则缺少对话框执行配置可能是导致错误的原因。
Always check the execution configurations to make sure the correct JRE folder is plugged in. In latest versions of jdk there is not a specific jre folder but the complete jdk package, so make sure it points at this folder.始终检查执行配置以确保插入了正确的 JRE 文件夹。在最新版本的 jdk 中,没有特定的 jre 文件夹,而是完整的 jdk 包,因此请确保它指向此文件夹。
you can define org.gradle.java.home
in ~/.gradle/gradle.properties
like:您可以在~/.gradle/gradle.properties
中定义org.gradle.java.home
,例如:
org.gradle.java.home=/path/to/jdk17
this will cover both gradle
cli and default value for IntelliJ (if not specified otherwise)这将涵盖gradle
cli 和 IntelliJ 的默认值(如果未另行指定)
(windows users file at C:\Users\<username>\.gradle\gradle.properties
) (Windows 用户文件位于C:\Users\<username>\.gradle\gradle.properties
)
I was running into this issue, only when building from terminal , while the execution from the IntelliJ Run Configuration works我遇到了这个问题,只有在从终端构建时,而从 IntelliJ 运行配置的执行工作
The reason is probably, that I had downloaded the JDK from the relatively new way of doing it directly from the IDE原因可能是,我已经从相对较新的方式直接从 IDE 下载了 JDK
My guess is, that this JDK with its path that is set here我的猜测是,这个 JDK 的路径设置在这里
Is only known to IntelliJ and likely dynamically passed to Gradle when running a run configuration, and not known to Gradle itself .只有 IntelliJ 知道,并且可能在运行运行配置时动态传递给 Gradle ,而Gradle 本身不知道。
I tried to figure out what is happening, so I've added this task which in both cases prints the same path to an older local JDK.我试图弄清楚发生了什么,所以我添加了这个任务,它在两种情况下都会打印到旧本地 JDK 的相同路径。
tasks.build.doFirst {
println "used jdk path: " + System.getenv('JAVA_HOME')
}
C:\Program Files\GraalVM\graalvm-ce-java11-21.1.0 C:\Program Files\GraalVM\graalvm-ce-java11-21.1.0
Afterwards I've deleted the old JDK under that path and retried both , run configuration and terminal execution.之后我删除了该路径下的旧 JDK 并重试了运行配置和终端执行。 Now the run configuration still works , while printing the same invalid JDK path , and the terminal execution fails with现在运行配置仍然有效,同时打印相同的无效 JDK 路径,并且终端执行失败并显示
ERROR: JAVA_HOME is set to an invalid directory: C:\Program Files\GraalVM\graalvm-ce-java11-21.1.0错误:JAVA_HOME 设置为无效目录:C:\Program Files\GraalVM\graalvm-ce-java11-21.1.0
Please set the JAVA_HOME variable in your environment to match the location of your Java installation.请在您的环境中设置 JAVA_HOME 变量以匹配您的 Java 安装位置。
I had expected the error in both cases, but it seems the JAVA_HOME directory is not necessarily used and in some way other than with the env variable, IntelliJ has to tell Gradle to use the right JDK from the .jdks directory .在这两种情况下我都预料到了错误,但似乎不一定使用JAVA_HOME 目录,并且除了使用 env 变量之外,IntelliJ 必须告诉 Gradle 使用.jdks 目录中的正确 JDK。
This is just an assumption, if anyone has an explanation I'd appreciate it, especially how Gradle is able to know what JDK to use.这只是一个假设,如果有人有解释我会很感激,尤其是 Gradle 如何知道要使用什么 JDK。
in build.gradle set sourceCompatibility = '11' not sourceCompatibility = '17'在 build.gradle 中设置 sourceCompatibility = '11' 不是 sourceCompatibility = '17'
Install java17.安装java17。
Then然后
File -> Project Structure -> Set sdk to java17文件 -> 项目结构 -> 将 sdk 设置为 java17
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.