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.

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.36</version>
</dependency>

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.

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: com.microsoft.sqlserver.jdbc.SQLServerDriver in Java [solution]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [steps]


No comments:

Post a Comment