Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

The Java Database Connectivity (JDBC) Application Programming Interface (API) provides a set of classes and interfaces. We can use these to develop applications to connect to data sources such as relational databases and process data. Further, we need a database-specific JDBC Driver that implements this API to connect to the specific database.

When connecting to the MySQL database using JDBC we could encounter the exception message java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver. When we use the previous, now deprecated class com.mysql.jdbc.Driver, we could get the message java.lang.ClassNotFoundException: com.mysql.jdbc.Driver.

In this tutorial, we’ll learn how to solve this exception. We don’t need any special setup other than the application development environment we may already use. We’ll discuss solving this exception using JUnit 5 integration tests with Apache Maven as the build tool.

2. Cause of the Exception

We get this exception when the driver manager service that manages and loads JDBC drivers can’t find a driver class needed to obtain a connection. This exception can only occur at run-time. We can request a specific MySQL JDBC driver class to be loaded with the Class.forName(“com.mysql.cj.jdbc.Driver”) call in the application; however, it’s unnecessary because the DriverManager locates and loads a suitable driver by itself at run-time.

However, when the DriverManger doesn’t find a suitable or requested driver class in the classpath, it throws this exception. The classpath is the directory path that the Java Runtime Environment (JRE) searches for classes. To fix this issue, we need to add the MySQL Connector/J binaries or the jar file containing the Connector/J classes to the runtime classpath.

3. Example

A standalone application doesn’t require an application/web server. We can fix this exception by adding the Maven dependency for MySQL Connector/J to the configuration file pom.xml:

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.0.33</version>
</dependency>

Let’s use a test class to check if the error is solved. No exception is thrown because we added the driver class and it gets found and loaded:

public class MySQLLoadDriverUnitTest {
    @Test
    void givenADriverClass_whenDriverLoaded_thenEnsureNoExceptionThrown() {
        assertDoesNotThrow(() -> {
            Class.forName("com.mysql.cj.jdbc.Driver");
        });
    }
}

4. Conclusion

In this article, we learned how to fix the java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver exception problem. We used a JUnit 5 unit test developed in a Maven project to demonstrate adding MySQL Connector/J to the runtime classpath to fix the exception.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW