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 i n 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
- 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 :
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 :
1. Select pom.xml from your Eclipse project package explorer
2. Go to the dependency tab as shown below
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]
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.
i face this problem
how to dolve on cmd
Just add the class in the classpath.
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.
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?
Sir I am getting module JDBC not found error while exucuting
Hello Sravani, which versin of Java are you using? From Java 9, JDBC is in separate module it not anymore with JDK
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
Can you provide more detail like how are you building is it Eclipse, Maven, ANT? most likely Java is picking the wrong classpath.
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.
Thanks It was helpful.
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
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.
use capital C in place of Class
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)<
Catch(ClassNotFoundException e )<
s.o.p(» Exception» + e.getMessage());
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.
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.
Can you paste your driver code and JAR file.
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
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.
Can you paste full error message including stack trace?
java.sql.SQLException: No suitable driver found for ‘jdbc:mysql://localhost:3306/mysql [Solution]
Make sure this JAR is available in classpath before running your Java program, otherwise Class.forName() will not be able to find and load the class and throw java.lang.ClassNotFoundException: com.mysql.jdbc.Driver, another dreaded JDBC error, which we have seen in the earlier post.
Recently I have seen a common pattern of this error where a Java developer running his program on a version higher than Java SE 6 expects that JDBC driver’s JAR will be automatically loaded by JVM because of autoloading of JDBC driver feature of JDBC 4.0 released in JDK 6 but misses the trick that the JDBC driver should also be JDBC 4.0 compliant like mysql-connector-java-5.1.18-bin.jar will be automatically loaded but older version may not, even if you run on Java 6.
So, make sure you have both JDK 6 and a JDBC 4.0 compliant driver to leverage the auto-loading feature of JDBC 4.0 specification. You can further see these free JDBC courses to learn more about JDBC 4.0 features.
How to reproduce the «No suitable driver found for ‘jdbc:mysql://localhost:3306/» Error in Java?
In order to better understand this error, let’s first reproduce this error by executing following Java program. I expect this program to throw the «No suitable driver found for ‘jdbc:mysql://localhost:3306/» error because I don’t have JDBC driver in the classpath.
You can see that we got the «No suitable driver found» error in JDBC. The reason was that JDBC API couldn’t find any Driver corresponding to «jdbc:mysql://» URL because we have not added the MySQL connector JAR which contains the JDBC driver required to connect to MySQL database.
How to solve «No suitable driver found for jdbc:mysql://localhost:3306/mysql»
You can solve this problem first by adding MySQL connector JAR, which includes JDBC driver for MySQL into classpath e.g. mysql-connector-java-5.1.18-bin.jar . It’s very easy, just download the JAR from MySQL website and drop it into your classpath. For Example, if you are running in Eclipse then you can drop it on the root of your project folder. Same is true for Netbeans, but if you are running your Java program from the command prompt then either use -cp option or set the CLASSPATH as described here.
I prefer -cp option because it’s simple and easy and you can see what is included in classpath right in the command line itself, no need to worry about whether JAR is included in CLASSPATH environment variable or not.
The error should go away just by adding JDBC driver in the classpath if you are running on Java 6, which supports JDBC 4.0 and the driver is also JDBC 4.0 compliant e.g. mysql-connector-java-5.1.36-bin.jar . From JDBC 4.0, Java has introduced auto loading of JDBC driver, hence you don’t need to load or register it manually using Class.forName() method.
If you are not running on Java SE 6 or your JDBC driver version doesn’t support JDBC 4.0 then just add the following line before calling DriverManager.getConnection() to load and register the MySQL JDBC driver. This will solve the problem (uncomment the line in above program):
This throws checked java.lang.ClassNotFoundException so makes sure you catch it. I have not caught it to keep the code clutter free by introducing try and catch statement.
1) Just add the MySQL JDBC JAR into classpath if you are running on Java SE 6 and driver is JDBC 4.0 compliant e.g. mysql-connector-java-5.1.36-bin.jar .
2) Alternatively, add the MySQL JDBC driver to classpath e.g. mysql-connector-java-5.1.18-bin.jar and call the Class.forName(«com.mysql.jdbc.Driver»); to load and register the driver before calling DriverManager.getConnection() method.
You can also check out JDBC API Tutorial and Reference (3rd Edition) to learn more about new features introduced in JDBC 3.0 and JDBC 4.0 specification and it is also one of the best books to learn JDBC API in Java.
That’s all about how to fix «No suitable driver found for jdbc:mysql://localhost:3306/mysql» error in Java. You can get this error from Eclipse or NetBeans IDE while connecting to local MySQL instance listening on default port 3306, don’t afraid, just follow the same approach. Drop the MySQL JDBC driver and call the Class.forName() method with the name of the class with implements Driver interface from JDBC API.
Related JDBC Tutorials for Java Programmers
- How to connect to MySQL database from Java Program (Guide)
- How to connect to Oracle database from Java (Guide)
- How to connect to Microsoft SQL Server from Java (Guide)
- How to setup JDBC connection Pool in Spring + Tomcat (Guide)
- 10 JDBC Best Practices Java Programmer Should Follow (see here)
- 6 JDBC Performance Tips for Java Applications (see here)