Solve Cannot access org.springframework.boot.SpringApplication, class file has wrong version xxx, should be xxx, please delete the file or make sure the file is in the correct classpath subdirectory.

1. Reproduce the error


I started the project today, but reported the following error:

insert image description here

Error:(3, 32) java: 无法访问org.springframework.boot.SpringApplication
  错误的类文件: /C:/Users/zxy/.m2/repository/org/springframework/boot/spring-boot/3.1.0/spring-boot-3.1.0.jar!/org/springframework/boot/SpringApplication.class
    类文件具有错误的版本 61.0, 应为 52.0
    请删除该文件或确保该文件位于正确的类路径子目录中。

2. Analysis errors


In the error message, the (wrong) class file version refers to java class file version.

According to the path of the above error message /C:/Users/zxy/.m2/repository/org/springframework/boot/spring-boot/3.1.0/spring-boot-3.1.0.jar, find it /spring-boot-3.1.0.jar, as shown in the following figure:

insert image description here

Copy spring-boot-3.1.0.jarto the desktop and unzip the jarpackage as spring-boot-3.1.0a folder at the same time:

insert image description here

Open spring-boot-3.1.0the folder, find org\springframework\bootthe directory, and use Windows PowerShellto enter the directory:

insert image description here

If you haven't installed it yet Windows PowerShell, you can click the tutorial on installing powershell on Windows .

We use the class file version number for javap -v SpringApplication.classviewing .SpringApplication.class

But before viewing, Windows PowerShellthe buffer size needs to be increased.

Right-click Windows PowerShellthe upper left corner of the screen, select properties, find the layout, and select the height of the screen buffer size, as shown in the following figure:

insert image description here

The default buffer size is 3000, which I set 9000.

Because SpringApplication.classthe number of bytes of the class file information exceeds the default buffer size, the complete class file information cannot be viewed.

Use the version number of the javap -v SpringApplication.classviewed SpringApplication.classclass file, as shown in the following figure:

insert image description here

As can be seen from the figure, SpringApplication.classthe class file version is 61, and the corresponding javaversion is 17.

Check out my jdkversion, pictured below:

insert image description here

My jdkversion is , and 8my SpringApplicationclass files require jdk.17

Therefore, my jdkversion is too low, and I can only lower SpringApplicationthe version to adapt to my jdkversion.

javajdkThere is a corresponding relationship between the version number of the class file and the version number, such as 61.0corresponding jdk17, 52.0corresponding jdk8.

The complete correspondence is shown in the table below:

Java version number java class file version
20 64
19 63
18 62
17 61
16 60
15 59
14 58
13 57
12 56
11 55
10 54
9 53
8 52
7 51
6 50
5 49
1.4 48
1.3 47
1.2 46
1.1 45
1.0.2 45

The complete correspondence can be found in: The class File Format

3. Fix bugs


As my jdkversion is 8, cannot support what is jdkrequired .17SpringApplication

Therefore, you can only reduce SpringApplicationthe version to adapt to my jdkversion.

For our modified SpringApplicationversion, we only need to modify the value under the label pom.xmlin the file .<parent><version>

Under my original label spring boot, as shown in the following code:<parent><version>3.1.0

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

Now modify <parent>the label under the<version>2.6.0

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

insert image description here

Look <parent>under the tab <version>for 2.6.0versions SpringApplication.class.

Find the package C:\Users\你的电脑名字\.m2\repository\org\springframework\boot\spring-boot\2.6.0in the directory spring-boot-2.6.0.jar, copy it to the desktop, and decompress it into spring-boot-2.6.0a folder.

In Windows PowerShell, use cd C:\Users\zxy\Desktop\spring-boot-2.6.0\org\springframework\bootthe command to enter spring-boot-2.6.0the folder and execute the following command:

insert image description here

The version of the class file <parent>under the label is , and the corresponding version is , which matches <version>the version I currently install .2.6.0SpringApplication.class52jdk8jdk

At this point, restart the project and it will run successfully, as shown in the following figure:

insert image description here

4. Error Summary


This error is generally reported: cannot be accessed org.springframework.boot.SpringApplication, the class file has the wrong version xxx, it should be xxx, please delete the file or ensure that the file is located in the correct class path subdirectory.

The reason is that your version is inconsistent spring bootwith your version. The solution is as follows:jdk

  1. Modify your jdkversion to adapt to your spring bootversion, which is generally impossible.

  2. Modify your spring bootversion, adapt your jdkversion, and often use this method.

Guess you like

Origin blog.csdn.net/lvoelife/article/details/130942659