0

I am currently learning Spring Boot and installed the extensions in VS Code. I used Spring Initializr and created Gradle Spring Boot project with Spring Web dependency. I tried to run it but it says:

Error: Main method not found in the file, please define the main method as: public static void main(String[] args)

This is my TestApplication.java:

package com.testing.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.2'
    id 'io.spring.dependency-management' version '1.1.2'
}

group = 'com.testing'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

I can see that public static void main(String[] args) does exist. Is something wrong with it or anything that is preventing me from running it?

I have no idea what to try to fix this. I wanted it to run.

gradlew bootrun

Starting a Gradle Daemon, 2 incompatible Daemons could not be reused, use --status for details

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'testing'.
> Could not resolve all files for configuration ':classpath'.
   > 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
      > 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')

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 9s

java --version

java 17.0.5 2022-10-18 LTS
Java(TM) SE Runtime Environment (build 17.0.5+9-LTS-191)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.5+9-LTS-191, mixed mode, sharing)

gradlew --version

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

Build time:   2023-07-10 12:12:35 UTC
Revision:     a38ec64d3c4612da9083cc506a1ccb212afeecaa

Kotlin:       1.8.20
Groovy:       3.0.17
Ant:          Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM:          11.0.13 (Oracle Corporation 11.0.13+8)
OS:           Windows 10 10.0 amd64
dsw
  • 3
  • 2
  • 1
    are there any Spring / maven / gradle configuration files you aren't showing us here? – starball Aug 21 '23 at 02:13
  • @starball i included the build.gradle. Anything else I needed to include? – dsw Aug 21 '23 at 02:20
  • How did you run this prograam? Are you using an IDE or command line? What were the exact steps or command you used? – Code-Apprentice Aug 21 '23 at 02:21
  • 1
    @Code-Apprentice As mentioned in the post, I used VS Code. While I'm inside the TestApplication.java, I clicked the Run Java button (top right play button) and the error comes up. – dsw Aug 21 '23 at 02:28
  • I'm not familiar with Spring Boot. The problem here might be due to the `@SpringBootApplication` annotation. – Code-Apprentice Aug 21 '23 at 02:33
  • can you read through https://github.com/microsoft/vscode-java-debug/issues/1207 and see if anything there solves your issue? 1) if workspace state is crashed, `Java: Clean Java Language Server Workspace` in command palette 2) You should not have both Extension Pack for Java and Java language Support extensions installed at the same time 3) check if your maven / gradle build works via commandline. If it doesn't work via commandline, then your project configuration has issues. – starball Aug 21 '23 at 02:36
  • What does `./gradlew bootRun` give you? – Slaw Aug 21 '23 at 03:14
  • Most probably you are trying to run some random run configuration with the top right play bottom. Try to use Run from the context menu (right mouse click). – Mar-Z Aug 21 '23 at 07:13
  • @Slaw i added to the post – dsw Aug 21 '23 at 07:36
  • @Mar-Z it's the same thing with same result – dsw Aug 21 '23 at 07:36
  • You have a class named `String` in your package. Remove it. – Luiggi Mendoza Aug 21 '23 at 07:40
  • @LuiggiMendoza I got the project from Spring Initializr and have done very minimal code change. I could not find any class named String in the project. – dsw Aug 21 '23 at 07:49
  • I replicated the same error in a new project some minutes before commenting by adding a `String` class on purpose (I faced this issue several years ago while studying Java). As a suggestion, try creating a new clean project without your code, then start moving pieces of your code into this new project until it fails. – Luiggi Mendoza Aug 21 '23 at 07:51
  • @LuiggiMendoza I have created a new (5th) clean project, added nothing to it and run it. Still the same issue. – dsw Aug 21 '23 at 08:07
  • Then there's an issue with your IDE configuration. Try using IntelliJ or Eclipse to run your project. – Luiggi Mendoza Aug 21 '23 at 08:16
  • 1
    @LuiggiMendoza I agree the OP has a configuration problem with the IDE. But the problem is that they don't seem to be delegating to Gradle, and are instead relying on the IDE's own build/execution system to run the project. If you delegate to Gradle, the project is pretty much IDE-independent. However, since executing the `bootRun` Gradle task also seems to fail, there's more wrong than just the IDE configurations. – Slaw Aug 21 '23 at 12:33
  • @dsw What does `java -version` and/or `./gradlew --version` give you? [This issue](https://github.com/gradle/gradle/issues/25869) looks to have the same error as you get with `bootRun`, and the cause appears to be too low a version of Java. Make sure you're using Java 17+ (or maybe just 11+?), and see if that solves the problem. – Slaw Aug 21 '23 at 14:50
  • @Slaw i have added those in the post at the bottom – dsw Aug 22 '23 at 05:17
  • 1
    It seems that my JAVA_HOME is set to JDK 11 so I installed JDK 19 and set the path appropriately. Executing `gradlew bootrun` in the terminal works now. However the problem with the VS Code still persist, but I only turned to VS Code because I was using IntelliJ and it wasn't working. Now after setting the appropriate path to JAVA_HOME, it is working as I hoped it would in IntelliJ so I will be continuing in that IDE instead of VS Code. – dsw Aug 22 '23 at 06:25
  • I prefer IntelliJ IDEA for Java development, though it can be a memory hog. If you want to try and stay with VS Code, make sure to have appropriate extensions for Java/Gradle(/Spring?) development installed. You should then be able to create a launch configuration that executes `./gradlew bootRun`. Note you should have IntelliJ doing something similar with a run configuration (that executes the `bootRun` task). – Slaw Aug 22 '23 at 13:56
  • I also find it strange that `java --version` was giving you `17.0.5`, but `./gradlew --version` showed Gradle was using Java `11.0.13`. – Slaw Aug 22 '23 at 13:57

0 Answers0