java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [Solution]

Problem : You are getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver fault piece connecting to MySQL database from Java Program. You may move running your Java application straight from ascendance prompt, trounce script, ANT or Eclipse.

Cause : In social club to connect to MySQL database, y'all postulate JDBC driver for MySQL. Influenza A virus subtype H5N1 flat which implements java.sql.Driver interface for MySQL. Every vendor is responsible to implement this flat for their databases. This driver implementation is provided past times MySQL every bit MySQL coffee connexion library. There is a flat called com.mysql.jdbc.Driver which implements this interface.


When y'all create Class.forName("com.mysql.jdbc.Driver") to charge together with register this driver class, the flat loader inward JVM search for this flat within all JAR files available inward CLASSPATH. If mysql-connector-java-5.1.25-bin.jar, which contains this flat is non available inward CLASSPATH together with then JVM volition throw java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at run-time. Remember, at that topographic point won't move whatever fault during compile fourth dimension because your computer program has no lead dependency to this JAR i.e. its non using whatever flat or method from this JAR directly. When Class.forName() method volition execute at run-time, it volition endeavor to discovery the driver flat provided every bit String declaration together with throw this fault if its non able to discovery it on classpath.


Solution : You tin shipping away create this fault past times deploying mysql-connector-java-5.1.25-bin.jar into your application's classpath. If y'all are non sure how to laid CLASSPATH, follow instructions given inward that article. Depending upon your construct tool y'all tin shipping away create next to create java.lang.ClassNotFoundException: com.mysql.jdbc.Driver inward Eclipse, Maven together with Gradle.




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

You postulate to add together MySQL JDBC driver inward your Eclipse Java project's classpath. MySQL driver is a type four JDBC driver, hence y'all only postulate to add together the JAR file inward Eclipse. Here are steps to add together an external JAR into Eclipse's Classpath

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


If y'all don't accept MySQL driver JAR, y'all tin shipping away download it from maven key library or straight from MySQL JDBC driver website.




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

You tin shipping away add together MySQL JDBC connexion driver past times adding every bit dependencies inward your gradle construct file every bit shown below :

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


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

Fixing this fault inward Maven is combat easy, only add together next dependency inward your project's pom.xml file.

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

Alternatively, if y'all are using Maven within Eclipse via M2Eclipse plugin together with then y'all tin shipping away also add together dependency every bit shown below :

Steps :
1. Select pom.xml from your Eclipse projection bundle explorer
2. Go to dependency tab every bit shown below
 fault piece connecting to MySQL database from Java Program java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [Solution]

3. Click add together together with search for MySQL connector, ane time works life pick out the correct version together with that dependency volition move added into Eclipse Classpath via Maven Dependency.
 fault piece connecting to MySQL database from Java Program java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [Solution]

If y'all don't run into  mysql-connector-java-5.1.36-bin.jar inside your Maven Dependency, only update your Maven projection or run maven install ascendance past times doing correct click on Maven Eclipse projection together with choosing Run As - Maven Install.


That's all almost how to create java.lang.ClassNotFoundException: com.mysql.jdbc.Driver fault inward Java. The fault should gone every bit shortly every bit y'all deploy MySQL connexion JAR file into CLASSPATH. IF y'all run into this fault fifty-fifty afterward deploying mysql-connector-java-5.1.25-bin.jar inward classpath together with then its sure as shooting an effect amongst classpath. It's possible that your classpath is non laid properly or your application's classpath is overridden past times around settings. depository fiscal establishment tally these steps for farther debugging.

If y'all similar this troubleshooting tips together with y'all are struggling amongst other ClassNotFoundException or NoClassDefFoundError, y'all tin shipping away also depository fiscal establishment tally next solutions :
  • How to fix 'javac' is non recognized every bit an internal or external command, operable computer program or batch file fault inward Java? [solution]
  • What is divergence betwixt NoClassDefFoundError together with ClassNotFoundException inward Java? [answer]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver inward Java? [solution]
  • How to fix java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener fault inward Spring? [solution]
  • How to bargain with java.lang.NoClassDefFoundError: org/dom4j/DocumentException inward Java?[solution]
  • Fixing Exception inward thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject inward Java? [solution]
  • Solving java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver inward Java [solution]
  • How to create java.lang.ClassNotFoundException: org.postgresql.Driver fault inward Java? [steps]

Subscribe to receive free email updates:

0 Response to "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [Solution]"

Posting Komentar