SpringBoot打包Jar启动报错:Failed to determine a suitable driver class
排查SpringBoot Jar启动时"Failed to determine a suitable driver class"错误
以下是针对该问题的具体排查步骤:
1. 检查数据库驱动依赖的打包范围
打开pom.xml,确认数据库驱动依赖的scope配置:
- 若直接引入驱动(如mysql-connector-java),确保
scope不是provided或test——这两个范围会导致依赖不被打进Jar包。正确配置可省略scope标签(默认compile):
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency>
- 若使用
spring-boot-starter-data-jpa,它会自动引入适配驱动,但需确认未手动排除驱动依赖。
2. 验证配置文件是否被正确打包
执行jar tf your-app.jar命令,查看输出中是否包含BOOT-INF/classes/application.properties(或你使用的其他配置文件)。若未找到,检查pom.xml的resources配置:
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.yml</include> </includes> </resource> </resources> </build>
3. 确认数据源配置的正确性
检查application.properties中的核心配置:
- 驱动类名称匹配数据库版本:MySQL 8.x用
com.mysql.cj.jdbc.Driver,5.x用com.mysql.jdbc.Driver;PostgreSQL用org.postgresql.Driver。 - 配置项名称无误,确保是
spring.datasource.driver-class-name,避免拼写错误。 - 确认
spring.datasource.url、spring.datasource.username、spring.datasource.password均已正确配置,无空值。
示例正确配置(MySQL 8.x):
spring.datasource.url=jdbc:mysql://localhost:3306/your_db?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update
4. 检查SpringBoot打包插件配置
确保pom.xml中配置了SpringBoot打包插件,否则生成的是普通Jar,无法正确加载依赖:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.7.12</version> <!-- 对应你的SpringBoot版本 --> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
配置后使用mvn clean package(或mvn clean install)打包,生成的Jar才是可执行的SpringBoot Jar。
5. 确认启动时的环境配置
若应用使用多环境配置(如application-dev.properties、application-prod.properties),启动Jar时需指定对应profile:
java -jar your-app.jar --spring.profiles.active=prod
未指定时,SpringBoot会加载默认的application.properties,若默认配置无数据源信息,会触发驱动类找不到的错误。
内容的提问来源于stack exchange,提问作者el alouchi mouhcine
相关产品推荐
相关产品推荐

