java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [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 command prompt, shell script, ANT or Eclipse.

Cause : In order to connect to MySQL database, you need JDBC driver for MySQL. A class which 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 instructions given in that article. Depending upon your build tool you can do following to fix java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse, Maven and Gradle.

Fixing java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in 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 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.

Further Learning
JSP, Servlets and JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 and 2
Java Platform: Working with Databases Using JDBC

If you like this 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: 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?

  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 ( in the folder "$CATALINA_HOME/lib" ( 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.

  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.