错误:java:包org.springframework.boot不存在。

7

今天我制作了一个简单的应用程序,但在运行时出现了一些错误。昨天我制作了另一个非常类似的应用程序,它一直工作得很好,但现在也出现了问题。

我遇到了以下这些错误:

Error:(4, 32) java: package org.springframework.boot does not exist
Error:(5, 46) java: package org.springframework.boot.autoconfigure does not exist
Error:(6, 35) java: package org.springframework.context does not exist
Error:(9, 2) java: cannot find symbol
  symbol: class SpringBootApplication
Error:(13, 17) java: cannot find symbol
  symbol:   class ApplicationContext
  location: class com.example.dependencyinjection.DependencyInjectionApplication
Error:(13, 42) java: cannot find symbol
  symbol:   variable SpringApplication
  location: class com.example.dependencyinjection.DependencyInjectionApplication
Error:(3, 38) java: package org.springframework.stereotype does not exist
Error:(5, 2) java: cannot find symbol
  symbol: class Controller

以下是我的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 https://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.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dependency-injection</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dependency-injection</name>
    <description>Example project for Spring Boot</description>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>1.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

我已经尝试过以下方法:

  • 重新导入所有的Maven项目
  • 生成源码并更新所有项目的文件夹
  • 使用Spring初始化器创建一个新项目
  • 检查settings.xml的路径设置
  • 删除.m2文件夹
  • 清除缓存并重新启动
  • 打开自动导入的项目
  • 移除/修改导入的VM选项
  • 强制Intellij重新读取所有的Maven依赖
  • 确认依赖项已在模块中
  • 更新Maven存储库
  • 许多不同的pom.xml配置
  • 克隆和构建已知可用的项目
  • 更改Maven的默认可执行文件
  • 完全关闭自动同步
  • 重新安装Intellij

还有其他的想法吗?


这个链接 https://youtrack.jetbrains.com/issue/IDEA-239150#focus=streamItem-27-4117814.0-0 有帮助吗?IntelliJ IDEA 2020.1 和 2020.1.1 版本存在一个特定于 Maven 项目的 bug,导致从 IDE 构建失败。您也可以使用 https://www.jetbrains.com/idea/download/previous.html 上的 2019.3.x 版本作为解决方法。 - CrazyCoder
添加 path.macros.xml 成功了!谢谢! - neovr
这个回答解决了你的问题吗?package org.springframework.boot does not exist - Gonçalo Peres
6个回答

7
在IntelliJ IDEA 2020.1和2020.1.1版本中存在一个已知的错误(将在2020.1.2中修复),编译器无法找到Maven依赖项,因为路径宏未被正确解析。
在修复发布之前,您可以使用解决方法降级到2019.3.x版本
解决方法是覆盖设置默认值,转到Settings(macOS上的Preferences)| Build, Execution, Deployment | Build Tools | Maven | 本地存储库(将其设置为与默认值不同的其他值)。
否则,请确保在 <IDE config>/options 目录中存在 path.macros.xml 文件,并包含以下内容:
<application>
  <component name="PathMacrosImpl">
   <macro name="KOTLIN_BUNDLED" value="<path to IDE installation>\plugins\Kotlin\kotlinc" />
    <macro name="MAVEN_REPOSITORY" value="<path to>/.m2/repository" />
  </component>
</application>

其中 <path to>/.m2/repository 是您本地Maven存储库的路径,<path to IDE installation> 是IDE安装目录的路径。


你指引了我正确的方向。我的问题得到解决,只需要将“Maven Home Path”更改为我本地安装的Maven,而不是使用默认的Wrapper即可。 - Khalil

3
在我的情况下,我改变了Maven引入JDK的版本。在IntelliJ最新版本中,它指向JDK11。然后将更改为我的内部JDK版本,问题得到解决。 enter image description here

1
我按照这些步骤操作后,我的项目成功构建了。
  1. 运行 mvn clean
  2. 前往 设置 -> 构建、执行、部署 -> Maven -> 仓库 并更新它们。(不确定是否必要)
  3. 通过单击maven选项卡上的重新加载按钮刷新您的maven依赖项。
  4. 进入构建并点击 重新构建项目。

1
将spring-boot-starter-web依赖项添加到您的POM中。
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

谢谢您的回答,但我确定它与pom文件无关。 - neovr

0

好的,我只需要重新启动我的笔记本电脑并设置所有必要的依赖项和JDK即可解决这个问题。


0

在你的pom.xml文件中,你不需要引入spring-boot的依赖。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>1.2.3.RELEASE</version>
        </dependency>

谢谢您的回答,但它仍然给我错误。 - neovr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接