Fix java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver Error in Java

Resolving java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver error by declaring dependency, including JAR in classpath, and updating MySQL connector.
On this page

Fix java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver Error in Java

Introduction

The java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver error is a common issue developers face when coding Java applications that interact with a MySQL database.

This exception occurs during runtime when the JVM tries to load the MySQL JDBC driver class but cannot find it in the classpath. Without the MySQL connector JAR containing this driver class, your application will fail to establish a connection to the database.

In this comprehensive guide, we’ll dig deeper into the various causes of this error and walk through solutions with detailed examples to help resolve it in all situations.

Common Causes

There are a few key reasons why the JVM may fail to locate the com.mysql.cj.jdbc.Driver class at runtime:

Missing Dependency Declaration in Build Tools

Nowadays, most Java projects use build tools like Maven or Gradle to manage dependencies. If you don’t declare the MySQL connector as a dependency in your pom.xml or build.gradle file, the JAR will not be downloaded when you build your project.

For example, without this dependency in Maven:

1<dependency>
2  <groupId>mysql</groupId>
3  <artifactId>mysql-connector-java</artifactId>
4  <version>8.0.32</version>
5</dependency>

The mysql-connector-java-8.0.32.jar will be missing from your project’s target dependency folder, causing the JVM to be unable to find the MySQL driver class.

Outdated Version of the MySQL Connector

Another common reason is having an outdated version of the MySQL connector library in your project’s build.

For example, you may have declared version 5.1.45 as the dependency, but this older version uses the class name com.mysql.jdbc.Driver instead of com.mysql.cj.jdbc.Driver. So even though the JAR is present, the JVM can’t find the expected driver class.

Always check Maven Central Repository for the latest version and keep your projects up-to-date.

JAR Not Added to Classpath Correctly

Finally, even if your project declares the dependency and downloads the JAR, you still need to configure your IDE, build, or server to put the JAR file in the classpath.

This means including it in the build path in Eclipse, putting it under the lib folder for Java EE servers like WildFly, or setting the JAR in the -cp classpath option when running Java apps from the command line.

If the JAR is not in the classpath at runtime, the JVM will throw the exception even if the JAR exists in your project dependencies.

Solutions and Examples

With an understanding of what causes this error, let’s go through solutions with concrete examples:

Declare Dependency in Build Tools

Maven

1<dependency>
2  <groupId>mysql</groupId>
3  <artifactId>mysql-connector-java</artifactId>
4  <version>8.0.32</version>
5</dependency>

Gradle

1implementation 'mysql:mysql-connector-java:8.0.32'

Then rebuild your project to fetch the JAR into the build output folder.

Manually Add JAR to Project

Eclipse

Right click project → Build Path → Configure Build Path

Under Libraries tab, click Add JARs and select the MySQL connector JAR

IntelliJ IDEA

File → Project Structure → Modules → Dependencies tab

Click + → JARs or directories → Choose JAR

Set JAR in Classpath

Executable JAR

1java -cp mysql-connector-java-8.0.32.jar:my-app.jar MainClass

Java EE Server (e.g. WildFly)

Place JAR under /standalone/deployments/lib folder

Add JAR to WEB-INF/lib

For Java web apps, place the JAR in WEB-INF/lib directory of project.

Troubleshooting Tips

  • Update build tools to fetch latest dependency versions
  • Check for conflicting duplicate JAR versions
  • Rebuild project and clean dependencies
  • Verify database connection URL, credentials
  • Use latest stable MySQL connector version

Summary

The java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver occurs when the MySQL JDBC driver JAR is missing from the classpath at runtime. Carefully declaring dependencies, manually including the JAR, and setting the classpath will help resolve this issue. Keeping your project dependencies up-to-date is key.