Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

Monday, July 26, 2021

How to Fix java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java [Solution]

Problem : You are getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error while connecting to MySQL database from Java Program. You may be running your Java application directly from the command prompt, shell script, ANT or Eclipse.

Cause : In order to connect to MySQL database, you need JDBC driver for MySQL. A class that implements java.sql.Driver interface for MySQL. Every vendor is responsible to implement this class for their databases. This driver implementation is provided by MySQL as MySQL java connector library. There is a class called com.mysql.jdbc.Driver which implements this interface.

When you do Class.forName("com.mysql.jdbc.Driver") to load and register this driver class, the class loader in JVM search for this class inside all JAR files available in CLASSPATH. If mysql-connector-java-5.1.25-bin.jar, which contains this class is not available in CLASSPATH then JVM will throw java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at run-time. 

Remember, there won't be any error during compile time because your program has no direct dependency to this JAR i.e. its not using any class or method from this JAR directly. When Class.forName() method will execute at run-time, it will try to find the driver class provided as String argument and throw this error if its not able to find it on classpath.

Solution : You can fix this error by deploying mysql-connector-java-5.1.25-bin.jar into your application's classpath. If you are not sure how to set CLASSPATH, follow the instructions given in that article. Depending upon your build tool you can do the following to fix java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse, Maven, and Gradle.

Fixing java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java and Eclipse

You need to add MySQL JDBC driver in your Eclipse Java project's classpath. MySQL driver is a Type 4 JDBC driver, so you just need to add the JAR file in Eclipse. Here are steps to add an external JAR into Eclipse's Classpath

Steps :
  • Right click on your project
  • Choose Build Path and choose configure build path option.
  • Choose Add External JARs option
  • Find and add mysql-connector-java-5.1.25-bin.jar into Eclipse's classpath. 

If you don't have MySQL driver JAR, you can download it from maven central library or directly from MySQL JDBC driver website.

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Gradle

You can add MySQL JDBC connector driver by adding as dependencies in your gradle build file as shown below :

dependencies {
 compile 'mysql:mysql-connector-java:5.1.+'

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Solution in Maven

Fixing this error in Maven is bit easy, just add following dependency in your project's pom.xml file.


Alternatively, if you are using Maven inside Eclipse via M2Eclipse plugin then you can also add dependency as shown below :

Steps :
1. Select pom.xml from your Eclipse project package explorer
2. Go to the dependency tab as shown below

How to fix java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Maven Eclipse

3. Click add and search for MySQL connector, once found choose the right version and that dependency will be added into Eclipse Classpath via Maven Dependency.

How to fix MySQL JDBC Driver Error in Java

If you don't see  mysql-connector-java-5.1.36-bin.jar inside your Maven Dependency, just update your Maven project or run maven install command by doing right click on Maven Eclipse project and choosing Run As - Maven Install.

That's all about how to fix java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error in Java. The error should gone as soon as you deploy MySQL connector JAR file into CLASSPATH. IF you see this error even after deploying mysql-connector-java-5.1.25-bin.jar in classpath then its certainly an issue with classpath. It's possible that your classpath is not set properly or your application's classpath is overridden by some settings. check these steps for further debugging.

If you like these troubleshooting tips and you are struggling with other ClassNotFoundException or NoClassDefFoundError, you can also check following solutions :
  • How to fix 'javac' is not recognized as an internal or external command, operable program or batch file error in Java? [solution]
  • What is difference between NoClassDefFoundError and ClassNotFoundException in Java? [answer]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • How to fix java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener error in Spring? [solution]
  • How to deal with java.lang.NoClassDefFoundError: org/dom4j/DocumentException in Java?[solution]
  • Fixing Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject in Java? [solution]
  • Solving java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver in Java [solution]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [steps]


  1. I am running Windows 10. JDK and JRE 8, MySQL - the workbench version is build 6.3.10 and the path to the connector is: C:\Program Files (x86)\MySQL\Connector J 8.0\
    the version that was installed by MySQL is mysql-connector-java-8.0.9-rc-bin.jar. Although I tried mysql-connector-java-5.1.46. I have been wrestling with this for a couple of days now without success - so I am missing something very simple. I am using notepad++ to build a simple test file (I do have Eclipse but I wanted it to work first in a more generic scenario) and compiling by command line. I have the path to the version 8 connector in my environmental variables (removed the path for the version 5.1.46, of course) - rebooted several times and checked that echo %PATH% had the path there. So the JVM has no excuse to not find the jar file.

    Just a little puzzled here - would appreciate any insight into the problem.

  2. Sir, I am working in eclipse to connect MySql through connector.jar library and I have import library successfully.It is not working still what should I do? Please give me full instructoins.

    1. Hello umar, what error you are getting? can you give more details like are you connecting to database using Java program? may be post the error and program itself?

    2. Sir I am getting module JDBC not found error while exucuting

    3. Hello Sravani, which versin of Java are you using? From Java 9, JDBC is in separate module it not anymore with JDK

  3. I find myself with classnotfound exception in jbdc driver and I don't know why,because I already added the jar file in the build path

    1. Can you provide more detail like how are you building is it Eclipse, Maven, ANT? most likely Java is picking the wrong classpath.

  4. If your server is tomcat, you can copy the "jar" file (https://dev.mysql.com/downloads/connector/j/) in the folder "$CATALINA_HOME/lib" (https://juanalbertogt.wordpress.com/2013/07/25/instalar-tomcat-7-en-centos-rhel-o-fedora/). Then you just have to restart the server and it should work. If it still doesn't work for you and you are programming a web server, you could put the source code that does all the database query work in the "jsp" file or in another "jsp" file included in it.

  5. i set the class path also
    while writting the code class.forName("com.mysql.jdbc.Driver "); error at class like Syntax error on token "class", invalid Expression can anyone slove it

    1. There is an space in your classname at the end com.mysql.jdbc.Driver ", I think you first correct that, it may help with the other error as well, if not then paste your full code and error and we can take a look here.

    2. use Class.forName("com.mysql.jdbc.Dirver");
      use capital C in place of Class

  6. I working on eclipse nd I have included MySQL library already ..
    But still show an exception com.mysql.jdbc.deiver
    Public class MySQLconnection{
    p s v m(String[] args){
    Class.forName("com.mysql.jdbc.driver ");
    S.o.p("Driver loaded");
    Catch(ClassNotFoundException e ){
    s.o.p(" Exception" + e.getMessage());

    1. It may not be in the classpath. If you can provide more details about your setup and how you are running the program, we may be able to locate the problem.

  7. I've added the jar file to the build path while connecting to the database using java program, still it shows the driver error,kindly help.
    "java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver"

    1. Can you paste your driver code and JAR file.

    2. To make a connection with JDBC you will need to add my SQL connector jar file into your java project https://www.fiverr.com/share/edjv53

  8. va.lang.NoClassDefFoundError: org/hibernate/Query
    Root Cause

    java.lang.ClassNotFoundException: org.hibernate.Query

  9. I am facing java.lang.ClassNotFoundException: com.mysql.jdbc.driver

    I have using mysql-connector-java.8.0.21.jar, tomcat server 9.
    My project is maven project and mysql-connector dependency is mentioned, and ofcourse all maven dependencies are mentioned in class path.

    FYI, i already tried by adding msql-connector.jar in tomcat server lib folder as per online comment.

    1. Can you paste full error message including stack trace?

    2. java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1412)
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220)
      at java.base/java.lang.Class.forName0(Native Method)
      at java.base/java.lang.Class.forName(Class.java:375)
      at com.wipro.util.DButil.getConnection(DButil.java:12)
      at com.wipro.Dao.AdminDao.checkAdmin(AdminDao.java:15)
      at com.wipro.loginServlet.LoginServlet.doPost(LoginServlet.java:57)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
      at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
      at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.base/java.lang.Thread.run(Thread.java:833)

    3. Can you check if mysql driver JAR file are in your classpath or not?

  10. bonsoir!!
    j'ai travaille sur Netbeans mon programme java et je voulais connecter a mysql database ..j'ai téléchargé le driver jdbc et j'ai importé le fichier dans la librairie de mon projet..mais tjrs l'erreur est " java.lang.ClassNotFoundException: com.jdbc.mysql.Driver"
    voici mon code :
    package twitterdb;
    import java.sql.*;
    import java.util.*;
    public class TwitterDb {
    public static void main(String[] args) {
    Connection cnx = connecterDB();
    public static Connection connecterDB() {
    Class.forName("com.jdbc.mysql.Driver"); // charger mysql
    System.out.println("Driver bien établi");
    String url="jdbc:mysql://localhost:3306/memoire";
    String nomUser="root";
    String password="";
    Connection cnx = DriverManager.getConnection(url,nomUser,password);
    System.out.println("bien connecté a votre base de données");
    return cnx;
    }catch (Exception e);
    return null ;
    s'il vous plait quelqu'un peut m'orienter a corrriger cet erreur

  11. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver getting this error even after adding external jar files please guide what to do

  12. I have this error : java.lang.ClassNotFoundException: com.mysql.jdbc.Driver how to solve it?

  13. I am unable to solve this error::java.lang.ClassNotFoundException: com.sql.jdbc.driver
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1372)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1195)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:377)

    I have pasted the following jars in the lib of my project:
    1.jsp-api 2.2.jar
    2.jstl 1.2.jar
    3.mysqlconnector-java 8.0.23.jar
    I also tried to conigure buildpath but the add external jars button is disabled and i am unable to add jars.
    I am using dynamic web project in eclipse and i am using tomcat server.

  14. Error occurred during initialization of boot layer
    java.lang.module.ResolutionException: Modules hibernate3 and hibernate.testing export package org.hibernate.testing.cache to module mysql.connector.java

    böyle bir hata alıyorum nasıl düzelteceğim konusunda yardımcı olabilir misiniz


Feel free to comment, ask questions if you have any doubt.