【问题标题】:Spring Boot application can't resolve the org.springframework.boot packageSpring Boot 应用程序无法解析 org.springframework.boot 包
【发布时间】:2016-01-22 21:58:00
【问题描述】:

我已经使用Spring Initializer建立了一个spring boot项目,我尝试了几次创建一个新项目或使用依赖项,一切似乎都到位了。

我正在使用 STS(Spring Tool Suite),它显示有关从org.springframework.boot 包导入的错误。运行应用程序会引发异常:

线程“main”java.lang.Error 中的异常:未解决的编译 问题:SpringApplication无法解析。

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>

我正在使用带有 STS 的 Java 1.8。

【问题讨论】:

标签: java spring maven spring-mvc


【解决方案1】:

当您将应用程序作为 jar 运行时,您的 Manifest.MF 文件应该知道哪个类具有 main 方法。

要在 SpringBoot 编译您的代码时添加此信息,请在您的 pom 文件中添加 start-class 属性。

例如:

<properties>
        <start-class>com.example.DemoApplication</start-class>
</properties>

【讨论】:

  • 这似乎解决了我的一个问题,太好了。但是现在它仍然无法解析 SpringApplication 类和注解。
  • 我之前遇到过这个问题,我尝试了三种不同的方法,所以我不太确定哪一种解决了这个问题:1 - 使用强制更新或快照/发布来更新你的 Maven 项目 2 - 重新启动你的 Spring工具套件 3 - 运行 Maven clean 命令。希望这会有所帮助。
  • 如果有一个类包含一个 main 你不需要它,因为它会被自动检测到。确保项目是作为 maven 项目添加的,而不是作为普通 java 项目导入的。
【解决方案2】:

项目右键 -> Maven -> 更新项目

然后选中“强制更新快照/版本”

【讨论】:

  • 就我而言,这是根本原因
【解决方案3】:

更改spring boot parent的版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
 </parent>

【讨论】:

  • 我在这上面花了 2 个小时。你让我开心。
  • 明确您需要设置最新版本的 Spring Boot 可能很有用。这很愚蠢,但我不得不检查问题中的 Spring Boot 版本,以确保它是关于更新版本而不是特定版本的 SB 的问题,因为答案没有解释问题所在。 .
  • 很好的解决方案,但原因是什么?
  • @Chalpat,关注official spring boot guide
【解决方案4】:

我在尝试使用基本的 Spring Boot 应用程序时遇到了同样的问题。我尝试使用来自官方spring-boot site的最新spring-boot版本。

截至今天,最新版本是1.5.4.RELEASE。但是无论对项目进行多次清理,我都会在我的 Eclipse IDE 上遇到编译问题。我将 spring-boot 的版本降低到 1.5.3.RELEASE 并且它只是 工作 !我早期的一些项目使用的是旧版本,这促使我尝试此选项。

【讨论】:

    【解决方案5】:

    从您的 pom.xml 中,尝试删除 spring 存储库条目,默认情况下将从 maven 存储库下载。我已经尝试过 1.5.6.RELEASE 并且效果很好。

    【讨论】:

      【解决方案6】:

      我在第一个 Spring 启动应用程序时遇到了同样的问题。

      在教程中我可以看到以下依赖项来启动示例应用程序

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

      我也这样做了,我的 Spring STS 正在识别所有类,但是当我用 @SpringBootApplication 注释我的主类时,它无法识别这个类,而我可以看到类路径中的 jar 可用。

      我有以下解决问题的方法

      • 将我的 Spring Boot 1.4.2.RELEASE 替换为 1.5.3.RELEASE
      • 右击项目-> Maven-> 下载资源
      • 右键项目->Maven->更新项目

      之后就成功了。

      谢谢

      【讨论】:

        【解决方案7】:

        如果以下步骤不起作用:

        将我的 Spring Boot 1.4.2.RELEASE 替换为 1.5.10.RELEASE

        • 右击项目-> Maven-> 下载资源
        • 右键项目->Maven->更新项目

        这个错误的原因可能是同一个版本的多个版本被下载到你的 maven 本地存储库文件夹中。

        因此,请按照以下步骤清除所有现有存储库 jar,并根据您的 POM.xml 中定义的依赖项从头开始下载所有内容。

        1. 转到构建路径。检查库添加部分中的 Maven 存储库。
        2. 选择任何 jar 和 mousehover .. 它将显示本地存储库位置。 一般是:user/.m2/repository/....
        3. 前往该地点。并删除包含的存储库文件夹。
        4. 现在右键单击您的项目.. 执行 Maven --> maven 更新。
          这将解决您的依赖问题。因为 Maven 将尝试再次从存储库下载所有项目并构建项目。

        【讨论】:

          【解决方案8】:

          本地 Maven 存储库中的 JAR 文件可能存在问题。尝试删除 .m2/repository 文件夹并再次点击 Maven -> Update Project... 以触发 Maven 再次下载依赖项。我试过了,它对我有用。

          【讨论】:

            【解决方案9】:

            在我的项目中,将 spring-boot-starter-parent 的依赖从 2.0.0.release 更新为 2.0.5.relese。 这样做解决了问题导入 org.springframework.boot.SpringApplication 无法解决

            【讨论】:

              【解决方案10】:

              不需要删除相对路径。只需更改 springframework boot 的 version parent。以下版本 2.1.2 可以成功运行。

              <?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>
              

              【讨论】:

                【解决方案11】:

                我的工作是通过添加

                <!-- 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>
                

                而不是直接使用其他主要依赖项,我不知道为什么。

                【讨论】:

                  【解决方案12】:

                  解决方法是在 pom.xml 文件中更改 Spring 的版本

                  <parent>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-parent</artifactId>
                      <version>1.5.9.RELEASE</version>
                  </parent>
                  

                  【讨论】:

                    【解决方案13】:

                    从 "C:\Users\usename.m2" 中删除存储库文件夹并重新创建或更新 maven 项目。

                    【讨论】:

                    • 这似乎更像是一条评论。请不要发布 cmets 作为答案。
                    【解决方案14】:

                    试试这个,它也可能对你有用。

                    1. 打开命令提示符并转到项目文件夹。
                    2. 构建项目(对于 Maven,运行 mvn clean install,对于 Gradle,运行 gradle clean build
                    3. 成功构建后,
                      • 在任何 IDE(intellij / eclipse)上打开您的项目。
                      • 如果 IDE 已打开,请重新启动它。

                    这在 v2.1 和 v2.4 上都适用于我

                    【讨论】:

                      【解决方案15】:

                      对于大多数读者来说,这个答案可能超出了主题。在我的情况下,依赖项没有更新,并且“mvn clean”没有工作,因为我在办公室的 wifi 网络高度安全,导致“连接超时”。 (同样尊重 github 推拉不起作用) 我刚开始用手机进行学习,它可以工作。 愚蠢,对大多数人来说超出主题,但它可能有助于一些非常具体的情况。

                      【讨论】:

                        【解决方案16】:

                        我在使用 STS 时遇到了这个问题。编辑了一些东西后,我看到了,一些工作区 创建项目的时候会出现这个问题,其他的不会。所以我只是在工作区创建一个新项目不会发生。

                        【讨论】:

                          【解决方案17】:

                          将 Spring boot 升级到最新版本 - 2.3.3.RELEASE 后。我也收到了这个错误 - Cannot resolve org.springframework.boot:spring-boot-starter-test:unknown。 Maven 全新安装、更新 Maven 项目、清理缓存无济于事。
                          解决方案是从 spring boot 父 pom 添加版本占位符:

                           <dependency>
                             <groupId>org.springframework.boot</groupId>
                             <artifactId>spring-boot-starter-test</artifactId>
                             <version>${spring-boot.version}</version>
                             <scope>test</scope>
                           </dependency>
                          

                          【讨论】:

                            【解决方案18】:

                            对我来说,删除.RELEASE这个词后就解决了

                            <version>2.5.3</version>
                            

                            而不是

                            <version>2.5.3.RELEASE</version>
                            

                            【讨论】:

                              猜你喜欢
                              • 2016-09-30
                              • 1970-01-01
                              • 2020-04-01
                              • 2020-12-24
                              • 2018-02-10
                              • 1970-01-01
                              • 2019-01-11
                              • 1970-01-01
                              • 1970-01-01
                              相关资源
                              最近更新 更多