Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Spring Boot cannot load the MySQL JDBC driver class

ErrorCannot load driver class: com.mysql.jdbc.Driver

Short answer

Spring Boot checks that the driver class you name can be loaded before it builds the connection pool. It fails when the MySQL driver jar is not on the classpath, or when the configured name is the old Connector/J 5 class. Add com.mysql:mysql-connector-j and either use com.mysql.cj.jdbc.Driver or drop the property.

  • Applies to Spring Boot 3.5, Java 21, Gradle 9, MySQL Connector/J 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the MySQL driver on the runtime classpath.

    ./gradlew dependencies --configuration runtimeClasspath | grep -i mysql
  2. 2Match what you see, then jump to its fix

    1. 1The first step prints nothing.The MySQL driver is not a dependencyFix
    2. 2application.properties contains spring.datasource.driver-class-name=com.mysql.jdbc.Driver.The properties name the old classFix
    3. 3No spring.datasource.url is set in any properties file or environment variable.The datasource URL is missing, so Boot looks for an embedded databaseFix

What does the error look like?

  • Cannot load driver class: com.mysql.jdbc.Driver

Cause 1 of 3

The MySQL driver is not a dependency

1Check

Spring does not ship JDBC drivers. If the jar is missing the class cannot load. Also check the dependency is not declared as compileOnly or testImplementation.

grep -n -i mysql build.gradle build.gradle.kts pom.xml 2>/dev/null

2Fix

Add the current Connector/J coordinates. The group is com.mysql and the artifact is mysql-connector-j. A Spring Boot project manages the version.

// build.gradle
runtimeOnly 'com.mysql:mysql-connector-j'

3Verify

The driver now appears on the runtime classpath.

./gradlew dependencies --configuration runtimeClasspath | grep -i mysql-connector

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

The properties name the old class

1Check

Connector/J 8 and later names its class com.mysql.cj.jdbc.Driver. Spring requires the named class to be loadable.

grep -rn "driver-class-name\|driverClassName" src/main/resources

2Fix

Use the new class name, or remove the property. Spring Boot can deduce the driver from the JDBC URL.

spring.datasource.url=jdbc:mysql://localhost:3306/app
spring.datasource.username=app
spring.datasource.password=change-me
# optional, can be omitted
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

3Verify

Start the app and check for the connection pool in the log.

./gradlew bootRun

Confirmed by docs.spring.io

Did this fix it?

Cause 3 of 3

The datasource URL is missing, so Boot looks for an embedded database

1Check

Without spring.datasource.url Spring Boot tries to auto-configure an embedded database, and fails if none is on the classpath.

grep -rn "spring.datasource" src/main/resources

2Fix

Set the URL, username and password for the MySQL server.

spring.datasource.url=jdbc:mysql://localhost:3306/app
spring.datasource.username=app
spring.datasource.password=change-me

3Verify

The app starts and connects.

./gradlew bootRun

Confirmed by docs.spring.io

Did this fix it?

How do I stop it coming back?

  • Leave driver-class-name out and let Spring Boot read it from the URL.
  • Declare the driver as runtimeOnly so it is in the packaged jar.
  • Use the Spring Boot dependency management so the driver version stays in step.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Spring Boot will not start because port 8080 is already taken

Action: Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.

Spring Boot listens on 8080 unless told otherwise, and only one process can own a port.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.