通過spring initializr創(chuàng)建的springboot項(xiàng)目,生成項(xiàng)目后無法正常運(yùn)行。
報(bào)錯(cuò)如下:
Error:(3, 32) java: 無法訪問org.springframework.boot.SpringApplication
錯(cuò)誤的類文件: /Users/domino/files/maven_repository/org/springframework/boot/spring-boot/3.0.4/spring-boot-3.0.4.jar!/org/springframework/boot/SpringApplication.class
類文件具有錯(cuò)誤的版本 61.0, 應(yīng)為 52.0
請刪除該文件或確保該文件位于正確的類路徑子目錄中。
spring-boot-starter-parent版本為3.0.4, jdk版本為1.8,默認(rèn)pom文件指定的jdk版本為 17。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
問題原因應(yīng)該為jdk的版本不兼容。jdk版本編碼及對應(yīng)關(guān)系可以參考網(wǎng)頁https://stackoverflow.com/questions/9170832/list-of-java-class-file-format-major-version-numbers
Java | Major version |
---|---|
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 |
其中52對應(yīng)的是jdk8,61對應(yīng)的是jdk17。那應(yīng)該是springboot 3.0.4版本使用的jdk17編譯的jar包,為了驗(yàn)證,將maven repository中的對應(yīng)jar包進(jìn)行解壓,并使用javap -v
命令查看編譯版本。
@localhost% javap -v /Users/domino/files/maven_repository/org/springframework/boot/spring-boot/3.0.4/spring-boot-3.0.4/org/springframework/boot/SpringApplication.class | grep major
major version: 61
可以驗(yàn)證猜想。
解決方法:將springboot的版本修改為3.0.0以下的版本,并將pom.xml中的java.version
的配置改為1.8。我修改為2.7.7版本以后clean項(xiàng)目后重新啟動(dòng)就可以正常啟動(dòng)了。
<?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.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.shutdown</groupId>
<artifactId>springboot01_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot01_demo</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
參考文檔:
-
https://blog.csdn.net/qq_51917985/article/details/128365245文章來源:http://www.zghlxwxcb.cn/news/detail-509101.html
-
https://stackoverflow.com/questions/9170832/list-of-java-class-file-format-major-version-numbers文章來源地址http://www.zghlxwxcb.cn/news/detail-509101.html
到了這里,關(guān)于Error-(3, 32) java- 無法訪問org.springframework.boot.SpringApplication問題解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!