繁体   English   中英

如何在 Spring Boot 项目中成功更改 Java 版本?

[英]How do I successfully change the Java version in a Spring Boot project?

我最近开始了一个新的 Spring Boot Gradle 项目,使用 IntelliJ。 在做工程的时候,我选择了Java 17作为工程的Java版本。 我的build.gradle文件有sourceCompatibility = '17' ,但我的JAVA_HOME环境变量设置为 Java 11,我真的不想使用 Java 17。

我尝试将sourceCompatibility更改为11 ,然后进入

文件 -> 项目结构 -> 项目设置 -> 项目

将 SDK 也更改为 11。

每当我在 IntelliJ 中单击“运行”按钮时,以及当我尝试在命令行中使用./gradlew clean build构建我的项目时,我都会收到一堆错误,主要是抱怨兼容性问题。 他们读的几乎一样; 这是失败构建错误的开始:

   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.0.1-SNAPSHOT.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.0.1-SNAPSHOT:20221222.181044-39
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.1-SNAPSHOT:20221222.181044-39 was found. The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.6' but:
          - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.1-SNAPSHOT declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 17 and the consumer needed a runtime of a component compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
          - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.1-SNAPSHOT declares a runtime of a component, and its dependencies declared externally:

我不想为了解决这个兼容性问题而重新开始我的项目。 我宁愿不必在 Java 版本之间切换(我知道 jEnv,只是宁愿暂时将计算机上的所有内容设置为 Java 11)。 有没有办法修复这个项目而不用破坏它并重做它?

需要 Java 17

Spring Boot 3 需要 Java 17 或更高版本。

请参阅Spring 博客页面,为 Spring Boot 3.0 做准备 报价:

Spring Boot 3.0 将需要 Java 17

另请注意,Spring Boot 3 是一项重大更新,其中的更改破坏了向后兼容性。 在这些更改中,包名称从javax.*更改为jakarta.*


你说:

我真的不想使用 Java 17

我无法想象为什么你会选择 Java 11 而不是 Java 17。

大多数在 Java 11 上运行的 Java 应用程序或库都应该在 Java 17 上运行。我不记得有任何有问题的变化,比如在 Java 8 到 9 到 11 之间。

Java 11 和 Java 17 都是长期支持 (LTS)版本。

Spring 3 需要 Java 17 Spring Doc 如果您想使用 Java 11,可以在 build.gradle 文件中降级 Springboot 版本 (org.springframework.boot)

IntelliJ IDEA > 首选项 > 构建、执行、部署 > Gradle > Gradle JVM -> java 11 photo x

要更改项目的 java 版本:

  1. 必须使用 JAVA_HOME 将 java 路径设置为环境变量路径,并在路径中进行相同的更改。
  2. 只需删除项目中的 .gradle 和 .idea 文件夹,然后使用相同的 IDE 重新启动项目。
  3. 重要的是,您必须更改与新 java 版本的源兼容性,并在项目结构中进行相同的更改并配置相同的 JDK 版本。

接受的答案对我不起作用。 即使设置了gradle JVM,重启IDE几次,还是不行。 唯一有效的解决方案是将 JAVA_HOME 更改为指向您首选的 java 版本的目录,然后重新启动 intellij。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM