Java:无法访问org.springframework.boot.SpringApplication 坏的类文件。

18
java: cannot access org.springframework.boot.SpringApplication
  bad class file: /C:/Users/xyz/.m2/repository/org/springframework/boot/spring-boot/3.0.0-SNAPSHOT/spring-boot-3.0.0-20220910.145857-773.jar!/org/springframework/boot/SpringApplication.class
    class file has wrong version 61.0, should be 52.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.
8个回答

19

将您的Spring Boot版本更改为上一个稳定版本。

之前

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

之后:

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

17

我们能否用OpenJDK版本来替换它? - White_King

8
如果您的计算机安装了Java 8,并且您访问Spring Initializer网站并通过选择Spring Boot 3.0.0(SNAPSHOT)作为应用程序并将打包方式选择为Jar,版本为8来创建一个应用程序,则生成的zip文件的pom中的版本将是11,即使您选择的版本是8。这意味着您的本地计算机上安装的是Java 8版本,而生成的zip文件的pom中的版本为11。

enter image description here


不要使用快照版本,因为快照版本目前正在由Spring Boot团队开发,创建项目时不应使用它们。请使用已发布的版本。 - Fazal Haroon
如果您想使用Spring Boot 3,则请选择Java版本17。 - Fazal Haroon

7
我把版本从2.7.5改为3.0.1,然后出现了这个问题。
java: cannot access org.springframework.boot.SpringApplication  bad class file

为了解决这个问题,Spring Boot 3 需要至少 Java 17 或更高版本的 Java。

我之前也遇到过这个问题,因为我使用的是 Java 11,所以当我在 pom.xml 中转换为 Java 17 后问题解决了。

  <properties>
    <java.version>17</java.version>
  </properties>

关于项目模块的项目结构以及项目完美运行。

注意:SNAPSHOT、M1、M2、M3和M4版本通常是正在进行中的工作。Spring团队仍在努力开发中,建议不要使用它们。

希望这能帮到你!如果有任何问题或需要其他帮助,请随时告诉我。

愉快编码! ‍


1
完美运行 - Fazal Haroon

4

从3.0.0版本降级到2.7.3版本后,我进行了更改,然后一切都正常了。


0

这里有几个可能会帮助您的解决方案

  1. 将Thymeleaf版本从3.0.0更改为3.1.0
<dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>3.1.0</version>
</dependency>
  1. 尝试在项目结构中更改您的SDK
  • 按下 Ctrl + Alt + Shift + S
  • 项目设置下选择项目
  • 将您的SDK更改为openjdk-17 Oracle OpenJDK Version 17.0.4openjdk-18 Oracle OpenJDK Version 18.0.2
  • 如果您没有它,请从Oracle网站下载并在SDK:下按添加SDK按钮
  • 任何高于17的SDK版本都可以使用
  • 应用,然后按确定
  • 右键单击pom.xml文件上的任何位置,然后选择Maven
  • 然后选择重新加载项目
  • 等待项目重新加载
  • 现在构建您的项目并运行它

0
尝试从pom.xml文件中更改你的Spring Boot版本。
我将版本从3.0.0更改为2.0.2.RELEASE,然后它正常运行。
问题发生的原因是Spring Boot版本3需要Java版本17才能运行,而你正在尝试使用Java版本8运行。

0
我遇到了这个错误。
java: cannot access org.springframework.boot.SpringApplication bad class file: /C:/Users/homa/.m2/repository/org/springframework/boot/spring-boot/3.1.3/spring-boot-3.1.3.jar!/org/springframework/boot/SpringApplication.class class file has wrong version 61.0, should be 55.0 Please remove or make sure it appears in the correct subdirectory of the classpath.

当我将版本11更改为17时,我的测试通过了。
<properties>
        <java.version>17</java.version>
    </properties>

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