SpringBoot导出JAR在CMD运行报无法确定合适驱动类错误
Spring Boot JAR启动报找不到JDBC驱动类问题排查
问题现象
- 本地VS Code环境内启动Spring Boot Rest API项目无运行时异常,Postman接口调用测试全部正常
- 通过VS Code导出项目JAR包后,在CMD命令行执行
java -jar xxx.jar启动时抛出运行时错误 - 核心错误日志:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
现有配置参考
pom.xml 核心依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency>
application.properties 配置
## Server Properties server.port= 8083 server.servlet.context-path= /TEST-API spring.datasource.url= jdbc:mysql://localhost:3306/TEST?useSSL=false&useLegacyDatetimeCode=false&zerodatetimebehavior=Converttonull spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username= root spring.datasource.password= 1234 spring.datasource.testWhileIdle = true spring.datasource.timeBetweenEvictionRunsMillis = 60000 spring.datasource.validationQuery = SELECT 1
根因分析
IDE启动正常、独立JAR启动报找不到驱动类,核心问题出在打包环节:
- VS Code默认的「导出JAR」功能是面向普通JavaSE项目设计的,不会自动把Maven引入的runtime依赖(比如mysql驱动)打入JAR包,也不会按照Spring Boot可执行FatJar的规范构建包结构
- IDE运行时会自动把Maven本地仓库里所有声明的依赖加入运行时类路径,所以不会报类找不到;独立JAR启动时只会加载JAR包内部包含的类,缺依赖、缺配置文件就会抛出该错误
解决步骤
替换打包方式,使用Spring Boot官方打包插件构建可执行JAR
不要用VS Code自带的导出JAR功能,先在pom.xml的<build><plugins>节点下添加官方打包插件,版本号和项目引入的Spring Boot版本保持一致:<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>配置完成后在项目根目录执行Maven打包命令:
mvn clean package -DskipTests,执行完成后target目录下会生成两个JAR文件,体积较大(通常几十MB)的就是包含所有依赖的可执行FatJar,后缀带.original的是未重打包的源文件,不能直接启动。校验JAR包内容完整性
用压缩软件打开打好的FatJar做两项检查:- 进入
BOOT-INF/lib/目录,确认存在mysql-connector-java-xxx.jar文件,不存在则说明依赖未被正确打入,检查Maven本地仓库的mysql驱动包是否损坏,重新执行mvn clean install拉取依赖后再打包 - 进入
BOOT-INF/classes/目录,确认存在application.properties文件,不存在则说明资源文件被过滤,在pom.xml的<build>节点下添加资源扫描配置:<resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> </resources>
- 进入
启动校验
进入target目录,执行java -jar 生成的jar包名.jar启动,即可正常加载驱动、连接数据库。如果仍有配置加载问题,可以把application.properties放到JAR包同目录,启动时手动指定配置路径:java -jar 生成的jar包名.jar --spring.config.location=./application.properties
内容的提问来源于stack exchange,提问作者user19439703KJ
相关产品推荐
相关产品推荐

