Spring Boot打包JAR后MySQL驱动类识别失败问题求助
问题场景
本地Eclipse中运行Spring Boot应用完全正常,但通过Maven导出JAR后启动时抛出如下错误:
Failed to determine a suitable driver class
完整异常信息:
Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class
现有配置
pom.xml 核心配置
<dependencies> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addClasspath>true</addClasspath> <mainClass>com.biswajit.RDSExample</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
application.properties 配置
spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.generate-ddl=true spring.jpa.show-sql=true spring.datasource.url=jdbc:mysql://database-2.**..amazonaws.com:3306/mydb spring.datasource.username=admin spring.datasource.password=***** spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
解决方法
1. 替换Maven打包插件(核心修复)
问题出在使用了maven-assembly-plugin打包,这个插件的打包逻辑和Spring Boot的可执行JAR规范不兼容——Spring Boot需要特定的类加载器结构来加载依赖,而assembly插件只是简单把所有依赖打包到一个JAR,会导致驱动类无法被正确识别。
移除maven-assembly-plugin,改用Spring Boot官方提供的spring-boot-maven-plugin,修改后的build部分如下:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.biswajit.RDSExample</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
2. 优化MySQL驱动依赖(可选)
可以去掉MySQL驱动的手动版本号,让Spring Boot的依赖管理自动适配兼容版本,避免版本冲突:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!-- 移除手动version,由Spring Boot parent管理 --> <scope>runtime</scope> <!-- 可选,明确驱动仅在运行时需要 --> </dependency>
3. 重新打包并启动
执行以下Maven命令完成打包:
mvn clean package
然后运行生成的可执行JAR(注意替换为实际的JAR文件名):
java -jar target/RDSExample-xxx.jar
内容的提问来源于stack exchange,提问作者Biswajit Tah

