0

I have a Spring Boot 3 project with Gradle and Java 17. I want to work with it using VS Code. I have the Java extension pack, the Spring Boot extension pack and the gradle extension in VS Code.

I can use the same project without problems in Eclipse. I also tried using a new project from start.spring.io. The projects work with the following configurations:

  • Maven + Java 11 + Spring Boot 2.7.14
  • Maven + Java 17 + Spring Boot 3.1.2
  • Gradle + Java 11 + Spring Boot 2.7.14

The log shows the following error, when i try to open the project:

!SESSION 2023-08-09 20:32:19.690 -----------------------------------------------
eclipse.buildId=unknown
java.version=17.0.7
java.vendor=Eclipse Adoptium
BootLoader constants: OS=linux, ARCH=x86_64, WS=gtk, NL=en_US

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-08-09 20:32:29.890
!MESSAGE Finished creating the Java project jdt.ls-java-project

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-08-09 20:32:31.142
!MESSAGE Importing Gradle project(s)

!ENTRY org.eclipse.jdt.ls.core 1 0 2023-08-09 20:32:31.143
!MESSAGE No previous Gradle project at /home/username/Documents/tmp/testb3j17g, it must be synchronized

!ENTRY org.eclipse.buildship.core 2 3 2023-08-09 20:32:42.643
!MESSAGE Synchronize project testb3j17g failed due to an error connecting to the Gradle build.
!STACK 0
org.gradle.tooling.BuildActionFailureException: The supplied phased action failed with an exception.
    at ...
Caused by: org.gradle.api.ProjectConfigurationException: A problem occurred configuring root project 'testb3j17g'.
    at ...
Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':classpath'.
    at ...
Caused by: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.1.2.
Required by:
    project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.1.2
Caused by: org.gradle.internal.component.NoMatchingConfigurationSelectionException: No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.1.2 was found. The consumer was configured to find a library for use during runtime, compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.2.1' but:
- Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.2 declares a library, packaged as a jar, and its dependencies declared externally:
    - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
    - Other compatible attribute:
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
- Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.2 declares a component for use during runtime, and its dependencies declared externally:
    - Incompatible because this component declares documentation and the consumer needed a library
    - Other compatible attributes:
        - Doesn't say anything about its target Java version (required compatibility with Java 11)
        - Doesn't say anything about its elements (required them packaged as a jar)
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
- Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.2 declares a library, packaged as a jar, and its dependencies declared externally:
    - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
    - Other compatible attribute:
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
- Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.2 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
    - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
    - Other compatible attribute:
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
- Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.2 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
    - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
    - Other compatible attribute:
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
- Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.2 declares a component for use during runtime, and its dependencies declared externally:
    - Incompatible because this component declares documentation and the consumer needed a library
    - Other compatible attributes:
        - Doesn't say anything about its target Java version (required compatibility with Java 11)
        - Doesn't say anything about its elements (required them packaged as a jar)
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
    at ...

!ENTRY org.eclipse.jdt.ls.core 4 0 2023-08-09 20:32:43.292
!MESSAGE Could not fetch model of type 'Map' using connection to Gradle installation '/usr/share/java/gradle'.
!STACK 0
org.gradle.tooling.BuildException: Could not fetch model of type 'Map' using connection to Gradle installation '/usr/share/java/gradle'.
    at ...
Caused by: org.gradle.internal.exceptions.LocationAwareException: A problem occurred configuring root project 'testb3j17g'.
    at ...
Caused by: org.gradle.api.ProjectConfigurationException: A problem occurred configuring root project 'testb3j17g'.
    at ...d$4(DefaultBuildTreeLifecycleController.java:98)
Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':classpath'.
    at ...
Caused by: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.1.2.
Required by:
    project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.1.2
Caused by: org.gradle.internal.component.NoMatchingConfigurationSelectionException: No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.1.2 was found. The consumer was configured to find a library for use during runtime, compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.2.1' but:
- Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.2 declares a library, packaged as a jar, and its dependencies declared externally:
    - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
    - Other compatible attribute:
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
- Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.2 declares a component for use during runtime, and its dependencies declared externally:
    - Incompatible because this component declares documentation and the consumer needed a library
    - Other compatible attributes:
        - Doesn't say anything about its target Java version (required compatibility with Java 11)
        - Doesn't say anything about its elements (required them packaged as a jar)
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
- Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.2 declares a library, packaged as a jar, and its dependencies declared externally:
    - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
    - Other compatible attribute:
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
- Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.2 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
    - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
    - Other compatible attribute:
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
- Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.2 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
    - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
    - Other compatible attribute:
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')
- Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.2 declares a component for use during runtime, and its dependencies declared externally:
    - Incompatible because this component declares documentation and the consumer needed a library
    - Other compatible attributes:
        - Doesn't say anything about its target Java version (required compatibility with Java 11)
        - Doesn't say anything about its elements (required them packaged as a jar)
        - Doesn't say anything about org.gradle.plugin.api-version (required '8.2.1')

After reading some other questions (like this one or that one), I thought gradle uses java 11 but gradle --version gives the following output:

$ gradle --version

------------------------------------------------------------
Gradle 8.2.1
------------------------------------------------------------

Build time:   2023-07-10 16:30:34 UTC
Revision:     <unknown>

Kotlin:       1.8.20
Groovy:       3.0.17
Ant:          Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM:          17.0.8 (Oracle Corporation 17.0.8+7)
OS:           Linux 6.4.4-arch1-1 amd64

I also set the VS Code setting Java › Import › Gradle › Java: Home to the JDK-17. What I find strange is that gradle build is working fine and also in the gradle view in VS Code the Spring Boot dependencies are listed.

Ole
  • 122
  • 1
  • 8
  • Did you configure `java.configuration.runtimes`? – JialeDu Aug 10 '23 at 08:35
  • Is that a Gradle or a VS Code setting? – Ole Aug 10 '23 at 10:17
  • This did the trick. I set the runtime configurations in VS Code and now it works. Thank you! Do you want to answer the question so that i can set it as the accepted answer? – Ole Aug 10 '23 at 19:04
  • I have made an answer. If you don't mind, you could click '✔' to mark my reply as the accepted answer. It will also help others to solve the similar issue. – JialeDu Aug 11 '23 at 01:05

1 Answers1

1

You can configure JDK for your project via java.configuration.runtimes configuration.

Example:

"java.configuration.runtimes": [
  {
    "name": "JavaSE-1.8",
    "path": "/usr/local/jdk1.8.0_201"
  },
  {
    "name": "JavaSE-11",
    "path": "/usr/local/jdk-11.0.3",
    "sources" : "/usr/local/jdk-11.0.3/lib/src.zip",
    "javadoc" : "https://docs.oracle.com/en/java/javase/11/docs/api",
    "default":  true
   },
   {
    "name": "JavaSE-12",
    "path": "/usr/local/jdk-12.0.2"
   },
   {
    "name": "JavaSE-13",
    "path": "/usr/local/jdk-13"
   }
]
JialeDu
  • 6,021
  • 2
  • 5
  • 24