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

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题求助(Maven依赖已配置)

Hey there! Let's troubleshoot this JDBC driver loading issue step by step. Even if Maven downloaded the jar, there are a few common pitfalls that might be causing this:

1. Check your dependency scope

Sometimes setting the mysql-connector-java dependency scope to provided or test will exclude it from your runtime environment. Make sure your dependency declaration looks like this (no unnecessary scope, or explicitly set to compile, which is the default):

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>your-version-number</version>
</dependency>
2. Verify the driver class name in Hibernate config

The driver class name changed between MySQL versions:

  • For MySQL 8.0+: Use com.mysql.cj.jdbc.Driver (note the cj prefix)
  • For MySQL 5.x: Use com.mysql.jdbc.Driver
    Double-check your Hibernate config file (like hibernate.cfg.xml or application.properties) to ensure the driver class matches the version you're using. Using the wrong class name for your driver version is a super common cause of this error.
3. Confirm the jar is in your runtime classpath

Just because Maven downloaded the jar doesn't mean your IDE or runtime environment is including it:

  • In IDEs like IntelliJ, go to File > Project Structure > Libraries and check if mysql-connector-java is listed.
  • If running via command line, use mvn exec:java or mvn spring-boot:run (if it's a Spring app) to ensure dependencies are pulled in. For packaged apps, unzip your war/jar and check if the driver jar exists in WEB-INF/lib (for wars) or BOOT-INF/lib (for Spring Boot jars).
4. Clean Maven cache and rebuild

Corrupted downloaded jars can cause this issue too. Run these commands to wipe old builds and re-download dependencies:

mvn clean install -U

The -U flag forces Maven to update snapshot dependencies, and clean removes stale build artifacts.

5. Check for dependency conflicts

Your project might have multiple versions of mysql-connector-java or conflicting JDBC drivers. Run this command to inspect your dependency tree:

mvn dependency:tree | grep mysql

If you see duplicate or mismatched versions, exclude the unwanted one from the parent dependency. For example:

<dependency>
    <groupId>some-other-dependency-group</groupId>
    <artifactId>some-other-dependency</artifactId>
    <exclusions>
        <exclusion>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </exclusion>
    </exclusions>
</dependency>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:53:55