Spring Boot cannot load the MySQL JDBC driver class
Cannot load driver class: com.mysql.jdbc.DriverShort 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.
Fix it fast
1Find out which cause you have
List the MySQL driver on the runtime classpath.
./gradlew dependencies --configuration runtimeClasspath | grep -i mysql2Match what you see, then jump to its fix
- 1
The first step prints nothing.The MySQL driver is not a dependencyFix - 2
application.properties contains spring.datasource.driver-class-name=com.mysql.jdbc.Driver.The properties name the old classFix - 3
No spring.datasource.url is set in any properties file or environment variable.The datasource URL is missing, so Boot looks for an embedded databaseFix
- 1
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/null2Fix
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-connectorConfirmed by dev.mysql.com
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/resources2Fix
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.Driver3Verify
Start the app and check for the connection pool in the log.
./gradlew bootRunConfirmed by docs.spring.io
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/resources2Fix
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-me3Verify
The app starts and connects.
./gradlew bootRunConfirmed by docs.spring.io
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:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Back end
A scanner flags jackson-databind: how to upgrade it in Gradle
jackson-databind vulnerability reported by a dependency scanner
Find where the old jackson-databind comes from, then raise it.
3 causes, each with a fixBack end
Gradle cannot resolve spring-boot-gradle-plugin after a Spring Boot 3 upgrade
Plugin [id: 'org.springframework.boot', version: '3.0.1'] was not found in any of the following sources
Spring Boot 3 needs Java 17 or newer and a recent Gradle, and its Gradle plugin is published on the Gradle Plugin Portal.
3 causes, each with a fixBack end
bootJar cannot find the main class of a Spring Boot project
Main class name has not been configured and it could not be resolved
The Spring Boot Gradle plugin looks in the main source set's output for a class with a public static void main method.
2 causes, each with a fixBack end
Spring Boot fails at startup with an error processing a condition on an auto-configuration
java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration
This message only says Spring Boot failed while evaluating an auto-configuration condition.
3 causes, each with a fixBack end
Swagger UI URL shows the Whitelabel Error Page in Spring Boot
Whitelabel Error Page when opening /swagger-ui.html
The Whitelabel page means Spring found no handler for that address.
3 causes, each with a fixBack 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