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:
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>
The driver class name changed between MySQL versions:
- For MySQL 8.0+: Use
com.mysql.cj.jdbc.Driver(note thecjprefix) - For MySQL 5.x: Use
com.mysql.jdbc.Driver
Double-check your Hibernate config file (likehibernate.cfg.xmlorapplication.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.
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 > Librariesand check ifmysql-connector-javais listed. - If running via command line, use
mvn exec:javaormvn 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 inWEB-INF/lib(for wars) orBOOT-INF/lib(for Spring Boot jars).
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.
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

