Home > database >  How do I successfully change the Java version in a Spring Boot project?
How do I successfully change the Java version in a Spring Boot project?

Time:12-23

I recently started a new Spring Boot Gradle project, using IntelliJ. When making the project, I chose Java 17 as the project Java version. My build.gradle file had sourceCompatibility = '17', but my JAVA_HOME environment variable is set to Java 11 and I don't really want to use Java 17.

I tried changing sourceCompatibility to 11, and went into

File -> Project Structure -> Project Settings -> Project

to change the SDK to 11 as well.

Whenever I click the "run" button in IntelliJ, as well as when I try to build my project in command line with ./gradlew clean build, I get a bunch of errors, mostly complaining about compatibility issues. They all read almost the same; here is the beginning of the errors from the failed 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:

I don't want to have to start my project over just to fix this compatibility issue. I'd prefer not to have to switch between Java versions (I know about jEnv, just prefer to leave everything on my computer set to Java 11 for now). Is there a way to fix this project without trashing it and redoing it?

CodePudding user response:

Java 17 required

Spring Boot 3 requires Java 17 or later.

See the Spring Blog page, Preparing for Spring Boot 3.0. To quote:

Spring Boot 3.0 will require Java 17

Also note that Spring Boot 3 is a major update, with changes that break backward compatibility. Amongst these changes is the change in package names from javax.* to jakarta.*.


You said:

I don't really want to use Java 17

I cannot imagine why you would choose Java 11 over Java 17.

Most any Java app or library that runs on Java 11 should run on Java 17. I do not recall any problematic changes like there were between Java 8 to 9 to 11.

Both Java 11 and Java 17 are long-term support (LTS) versions.

CodePudding user response:

Spring 3 requires Java 17 Spring Doc. You can downgrade Springboot version (org.springframework.boot) in build.gradle file, if you want to work with Java 11

CodePudding user response:

IntelliJ IDEA > Preferences > Build, Execution, Deployment > Gradle > Gradle JVM -> java 11 photo x

  • Related