简体   繁体   English

IntelliJ - 无效的源版本:17

[英]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

Set Gradle JVM to target build jvm将 Gradle JVM 设置为目标构建 jvm

File -> Settings.. ->文件 -> 设置.. -> 在此处输入图像描述

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:以下是要遵循的步骤:

  • Close IntelliJ IDEA关闭IntelliJ IDEA
  • Open a terminal window and check your JAVA_HOME variable value:打开一个终端窗口并检查您的JAVA_HOME变量值:
    • *nix system: echo $JAVA_HOME *nix 系统: echo $JAVA_HOME
    • Windows system: echo %JAVA_HOME% Windows 系统: echo %JAVA_HOME%
  • The JAVA_HOME path should be pointing to a different path, then set it to the openjdk-17 path: JAVA_HOME路径应该指向不同的路径,然后将其设置为openjdk-17路径:
    • *nix system: export JAVA_HOME=/path/to/openjdk-17 *nix 系统: export JAVA_HOME=/path/to/openjdk-17
    • Windows system: set JAVA_HOME=path\to\openjdk-17 Windows系统: set JAVA_HOME=path\to\openjdk-17
  • Open your project again in IntelliJ IDEAIntelliJ IDEA中再次打开您的项目
  • Make sure to set both source and target compatibility versions (not only the sourceCompatibility )确保设置目标兼容性版本(不仅是sourceCompatibility

You should be able to build your project.您应该能够构建您的项目。

EDIT: Gradle Toolchain编辑:摇篮工具链

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

A picture is worth a thousand words!一张图片胜过千言万语!

Enter to preferences and change Gradle JVM.输入首选项并更改 Gradle JVM。

在此处输入图像描述

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 引发此错误的原因可能有很多

  • Check your JAVA_HOME ->检查你的 JAVA_HOME ->
echo $JAVA_HOME

if JAVA_HOME is not correct, update the JAVA_HOME environment variable.如果 JAVA_HOME 不正确,请更新 JAVA_HOME 环境变量。

  • check if the project structure in IntelliJ is correct.检查 IntelliJ 中的项目结构是否正确。
File > Project Structure > Project Setting > Project -> SDK and Language level should match

File > Project Structure > Project Setting > Modules -> Language Level should match with SDK
  • Check and fix settings in preferences are correct检查并修复首选项中的设置是否正确
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.

相关问题 IntelliJ:Scala:Javac:无效的源版本:1.7 - IntelliJ: scala: javac: invalid source release: 1.7 Java 17:java:带有--enable-preview的无效源版本7(预览语言功能仅支持版本17) - Java 17: java: invalid source release 7 with --enable-preview (preview language features are only supported for release 17) intellij 想法 - 错误:java:源代码版本 1.9 无效 - intellij idea - Error: java: invalid source release 1.9 错误:java:无效的源版本:Intellij 中的 8。 这是什么意思? - Error:java: invalid source release: 8 in Intellij. What does it mean? IntelliJ IDEA - java:错误:无效源版本:18 - IntelliJ IDEA - java: error: invalid source release: 18 Java:无效的源版本:9 - Java: invalid source release: 9 错误:java:错误:源代码版本无效:13 使用带有 IntelliJ 的 JDK12 - Error:java: error: invalid source release: 13 using JDK12 with IntelliJ IntelliJ 2021.2.2 在编译程序时给出错误“java:错误:无效的源版本:18” - IntelliJ 2021.2.2 gives error 'java: error: invalid source release: 18' when compiling program IntelliJ 使用错误的 JDK - 任务“:compileJava”执行失败。 &gt; 无效的源版本:11 - IntelliJ Using Wrong JDK - Execution failed for task ':compileJava'. > invalid source release: 11 Maven 编译,CircleCI 构建失败,因为目标版本 17 无效 - Maven compile, CircleCI build failing because of invalid target release 17
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM