You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Spring Boot 2.3.0无法加载org.postgresql.Driver驱动问题求助

Fixing "Cannot load driver class: org.postgresql.Driver" in Spring Boot 2.3.0.RELEASE

Hey there, let's work through this PostgreSQL driver loading issue you're hitting with your Spring Boot app. I've reviewed your pom.xml and setup, so here are practical steps to resolve the problem:

1. Verify PostgreSQL Dependency Presence & Scope

First, let's make sure the PostgreSQL driver is actually being pulled into your project and isn't restricted by a scope:

  • Run this Maven command to check your dependency tree and confirm the postgresql jar is present (no exclusions blocking it):
    mvn dependency:tree | grep postgresql
    
  • Ensure your postgresql dependency doesn't have a <scope>provided</scope> tag—this would exclude it from runtime, which is exactly why the driver can't be loaded. Your current pom doesn't have this, but it's worth double-checking if you made any changes.

2. Validate Your application.properties Configuration

Spring Boot 2.3+ can auto-detect the PostgreSQL driver from your JDBC URL, but misconfigured properties can still cause issues:

  • Make sure your datasource settings are correct (replace placeholders with your actual DB details):
    spring.datasource.url=jdbc:postgresql://localhost:5432/your_database_name
    spring.datasource.username=your_db_user
    spring.datasource.password=your_db_password
    # Optional, but if you specify it, ensure it's exact:
    spring.datasource.driver-class-name=org.postgresql.Driver
    
  • Double-check the URL for typos (like missing postgresql:// prefix, wrong port, or incorrect database name).

3. Clean Maven Caches & Rebuild

Corrupted or outdated Maven dependencies are a common culprit. Try cleaning and refreshing your build:

  • Run this command to clean your project, update dependencies, and rebuild:
    mvn clean install -U
    
  • The -U flag forces Maven to update snapshot dependencies, ensuring you get a fresh, uncorrupted copy of the PostgreSQL driver.

4. Sync Dependencies in Your IDE

Sometimes IDEs fail to sync Maven changes, leaving outdated dependencies in your classpath:

  • IntelliJ: Click the "Reload All Maven Projects" button in the Maven tool window, or go to File > Invalidate Caches... (check "Clear file system cache and local history") and restart the IDE.
  • Eclipse: Right-click your project > Maven > Update Project... (check "Force Update of Snapshots/Releases") and apply.

5. Check if the Driver is Included in Your Built Jar (If Running a Jar)

If you're packaging your app as an executable Jar, confirm the PostgreSQL driver is bundled inside:

  • Run this command to list the contents of your built Jar and look for the driver class:
    jar tf target/cab-booking-0.0.1-SNAPSHOT.jar | grep org/postgresql/Driver.class
    
  • If you don't see the class listed, your dependency isn't being packaged correctly—go back to step 1 to fix the dependency configuration.

6. Explicitly Pin the Driver Version (Optional)

While Spring Boot 2.3.0.RELEASE manages PostgreSQL driver version 42.2.12 by default, you can try explicitly defining the version to rule out any version resolution issues:

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

If you suspect a conflict with Spring Boot's dependency management, you can add an exclusion (though this is rarely needed):

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.12</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
        </exclusion>
    </exclusions>
</dependency>

If none of these steps resolve the issue, share the full stack trace from your error logs (not just a screenshot)—this will help pinpoint any hidden classpath or configuration issues.

内容的提问来源于stack exchange,提问作者Rupesh Patil

火山引擎 最新活动