Spring Boot application can't resolve the org.springframework.boot package
Asked Answered
C

21

43

I have set up a spring boot project using the Spring Initializer, I tried several times to create a new project or play with the dependencies, everything seems to be in place.

I am using STS(Spring Tool Suite) and it shows errors about the imports from theorg.springframework.boot package. Running the application throws an exception:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: SpringApplication cannot be resolved.

com.example.DemoApplication:

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

@SpringBootApplication
public class DemoApplication {

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

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <start-class>com.example.DemoApplication</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

I am using Java 1.8 with STS.

Camper answered 23/10, 2015 at 11:18 Comment(1)
Possible duplicate of @SpringBootApplication cannot be resolved to a type In STSPronominal
U
39

change the version of spring boot parent

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
 </parent>
Ulises answered 24/3, 2017 at 11:19 Comment(5)
I spent 2 hours on this. YOu made my day.Paction
It might be useful to precise that you need to set the latest version of Spring Boot. This is stupid, but I had to check the version of Spring Boot in the question just to be sure that it was about updating the version and not an issue with a specific version of SB, because the answer doesn't explain what's wrong...Hourihan
Good solution but the reason?Hashim
@Hashim , Follow official spring boot guide.Clementina
Changing to spring boot version 2.7.3 fixed my issue.Steamroller
R
30

Right button on project -> Maven -> Update Project

then check "Force update of Snapshots/Releases"

Rang answered 7/9, 2016 at 19:32 Comment(1)
In my case, it was the root causeRubenstein
H
9

If the below step not work:

Replaced my Spring Boot 1.4.2.RELEASE to 1.5.10.RELEASE

  • Right click on project and -> Maven-> Download Resources
  • right click on project -> Maven-> Update project

The reason for this error might be multiple version of the same is downloaded into your maven local repository folder.

So follow below steps to clear all existing repository jars and download all from beginning based on dependencies defined in your POM.xml..

  1. Go to build path . Check Maven Repository in the libraries added section.
  2. Choose any jar and mousehover .. it will show the local repository location. generally it is : user/.m2/repository/....
  3. Go to the location . and remove the repository folder contains.
  4. Now right click on your project .. do Maven --> maven update.
    This will solve your dependencies problem . as Maven will try to download all the items again from repository and build the project.
Holman answered 6/2, 2018 at 1:48 Comment(0)
C
6

Try this, It might work for you too.

  1. Open command prompt and go to the project folder.
  2. Build project (For Maven, Run mvn clean install and For Gradle, Run gradle clean build)
  3. After successful build,
    • Open your project on any IDE (intellij / eclipse).
    • Restart your IDE incase it is already opened.

This worked for me on both v2.1 and v2.4

Caenogenesis answered 2/5, 2019 at 7:20 Comment(0)
W
4

From your pom.xml, try to remove spring repository entries, per default will download from maven repository. I have tried with 1.5.6.RELEASE and worked very well.

Wane answered 7/8, 2017 at 3:18 Comment(0)
C
3

In my project, updated the dependency on spring-boot-starter-parent from 2.0.0.release to 2.0.5.relese. Doing this resolved the issue The import org.springframework.boot.SpringApplication cannot be resolved

Concavity answered 11/10, 2018 at 0:25 Comment(0)
D
2

Solution is change the version of Spring in file pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
Darken answered 13/4, 2019 at 15:37 Comment(0)
A
1

When you run your application as an jar, your Manifest.MF file should know which class has the main method.

To add this information when SpringBoot compiles your code, add start-class property on your pom file.

E.g.:

<properties>
        <start-class>com.example.DemoApplication</start-class>
</properties>
Anhanhalt answered 23/10, 2015 at 11:28 Comment(3)
This seems to fix one of my problems, great. But now it still can't resolve the SpringApplication class and the annotation.Camper
I had this problem before and I tried three differents things so I'm not quite sure which one fixed the problem: 1 - Update your Maven Project with force update or snapshots/release 2 - Restart your Spring Tool Suite 3 - Run Maven clean command. Hope this helps.Anhanhalt
If there is a single class containing a main you don't need it as it will be auto detected. Make sure the project is added as a maven project and not that you have imported it as a normal java project.Ideomotor
M
1

I had the same problem when I was trying to work with a basic spring boot app. I tried to use the latest spring-boot version from the official spring-boot site.

As on today, the latest version is 1.5.4.RELEASE. But I keep getting compilation issues on my eclipse IDE irrespective of multiple cleanups to the project. I lowered the version of spring-boot to 1.5.3.RELEASE and it simply worked !! Some of my earlier projects were using the old version which drove me to try this option.

Memberg answered 24/7, 2017 at 21:8 Comment(0)
S
1

I have encountered the same problem while my first Spring boot application.

In tutorial i could see following dependency to start sample application

  • Spring Boot 1.4.2.RELEASE
  • Maven 3
  • java 8

I have done the same, my Spring STS is recognizing all class but when i am annotating my main class with @SpringBootApplication it's not recognizing this class whereas i could see jar was available in the class path.

I have following to resolve issues

  • Replaced my Spring Boot 1.4.2.RELEASE to 1.5.3.RELEASE
  • Right click on project and -> Maven-> Download Resources
  • right click on project -> Maven-> Update project

After that it worked.

Thanks

Sexpot answered 11/9, 2017 at 18:35 Comment(0)
W
1

Mine worked by adding

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>

instead of directly using other main dependencies, I have no idea why.

Winebibber answered 4/3, 2019 at 15:39 Comment(0)
A
1

After upgrading Spring boot to the latest version - 2.3.3.RELEASE. I also got this error - Cannot resolve org.springframework.boot:spring-boot-starter-test:unknown. Maven clean install, updating maven project, cleaning cache do not help.
The solution was adding version placeholder from spring boot parent pom:

 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <version>${spring-boot.version}</version>
   <scope>test</scope>
 </dependency>
Armington answered 4/9, 2020 at 12:52 Comment(0)
C
1

For me it was solved after removed the word .RELEASE

<version>2.5.3</version>

instead of

<version>2.5.3.RELEASE</version>
Carve answered 16/8, 2021 at 12:56 Comment(0)
P
0

Maybe there is a problem with the JAR files in the local Maven repository. Try deleting the .m2/repository folder and hit Maven -> Update Project... another time to trigger Maven to download the dependencies again. I tried it and it worked for me.

Passel answered 3/9, 2018 at 19:51 Comment(0)
L
0

It's not necessary to remove relative path. Just change the version parent of springframework boot. The following version 2.1.2 works successfully.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.appsdeveloperblog.app.ws</groupId>
    <artifactId>mobile-app-ws</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mobile-app-ws</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.appsdeveloperblog.app.ws</groupId>
            <artifactId>mobile-app-ws</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Lark answered 19/2, 2019 at 9:33 Comment(0)
C
0

This answer may be out of topic for most of readers. In my case the dependency didn't update and "mvn clean" didn't work since my wifi network at the office is highly securised, leaving a "connection timed out". (same respect github pushes and pulls don't work) I just moved to teathering with my phone and it works. Stupid, out of topic for most, but it may help some very specific cases.

Commentate answered 13/8, 2019 at 8:13 Comment(0)
B
0

I have this problem when using STS. After edited something, I see that, some workspaces when create a project will happen this problem, and others will not. So I just create a new project in workspaces will not happen.

Bounty answered 28/10, 2019 at 8:3 Comment(0)
S
0

add ${project.parent.version} in build - plugin section for spring-boot-maven-plugin and load changes in pom.xml

<build>
    <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${project.parent.version}</version>
            </plugin>
    </plugins>
</build>
Swordbill answered 27/9, 2022 at 10:29 Comment(0)
U
0

I was constantly getting the same error. I was getting error after plugin updates and eclipse update. I had spring boot and necessary plugins installed in eclipse. But the option to create a spring boot project was not appearing. I understood that it was not seeing the spring boot plugin. I also deleted eclipse. I reinstalled eclipse. I reinstalled the plugins. Problem solved. The simplest solution. Solution -> Delete Eclipse ->(Optional Delete .m2,.p2,etc.) -> Install Eclipse -> Installing required plugins.(eclipse web developer tools, eclipse m2e,spring tools).

Unpeopled answered 3/11, 2022 at 8:53 Comment(0)
A
0

In my case it was silly mistake I added dependency from Spring initializer where I mistook choosing gradle project instead of maven Project

Aranyaka answered 23/11, 2022 at 8:14 Comment(0)
U
-1

Delete the repository folder from "C:\Users\usename.m2" and create again or update maven project.

Umbilicate answered 26/4, 2019 at 20:59 Comment(1)
This seems to be more like a comment. Please do not post comments as answers.Royo

© 2022 - 2024 — McMap. All rights reserved.