You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot项目运行编译Jar时出现无法确定合适驱动类错误

解决Spring Boot可执行Jar运行时找不到PostgreSQL驱动的问题

问题重现

使用Java Spring Boot开发REST API,在VS Code中运行代码正常,但运行编译后的.jar文件时抛出以下错误:

<message>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0: 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</message>
    <frame>
      <class>org.springframework.beans.factory.support.ConstructorResolver</class>
      <method>createArgumentArray</method>
      <line>798</line>
    </frame>

项目使用PostgreSQL数据库,已将postgresql依赖的scope从runtime改为默认,但问题依旧。

相关配置:

修改后的postgresql依赖

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

application.properties

spring.datasource.url = jdbc:postgresql://localhost:5432/languagehub
spring.datasource.username = [USER]
spring.datasource.password = [PASSWORD]
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=org.postgresql.Driver
server.port = 8080
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

解决方案

1. 确保Spring Boot Maven插件配置正确

如果pom.xml未配置spring-boot-maven-plugin或配置有误,打包出的jar可能不会包含依赖库。添加或检查以下配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

该插件负责生成可执行的fat jar,会将所有依赖(包括PostgreSQL驱动)打包到jar内部的BOOT-INF/lib目录中。

2. 执行正确的打包命令

使用Maven的repackage目标确保生成可执行jar:

mvn clean package spring-boot:repackage

避免直接使用mvn clean package,若插件配置存在问题,该命令可能仅生成普通jar而非fat jar。

3. 验证驱动是否被包含在jar中

解压生成的jar文件,检查BOOT-INF/lib目录下是否存在类似postgresql-xxx.jar的文件。若不存在,说明依赖未被正确打包,需检查pom.xml的依赖配置是否存在冲突,或是否有其他插件排除了该依赖。

4. 检查数据库连接配置正确性

  • 确认spring.datasource.url拼写无误,比如jdbc:postgresql://localhost:5432/languagehub中的数据库名称languagehub是否存在,端口5432是否为PostgreSQL实际运行端口。
  • 确保运行jar的环境能访问localhost的5432端口(本地运行时需确认PostgreSQL服务已启动)。
  • 可尝试移除spring.datasource.driver-class-name配置,Spring Boot能从jdbc url自动识别驱动类,避免手动配置可能出现的类名错误。

5. 显式指定PostgreSQL依赖版本

默认依赖版本可能存在兼容性问题,显式指定一个稳定版本:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.6.0</version> <!-- 替换为稳定版本 -->
</dependency>

内容的提问来源于stack exchange,提问作者Alan Hart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 22:27:52