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

JavaJdbcMysql Connector

Java Problem Overview


What is wrong with the code there are lots of error while debugging. I am writing a code for a singleton class to connect with the database mysql.

Here is my code

package com.glomindz.mercuri.util;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySingleTon {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "test";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "";

    private static MySingleTon myObj;	
    private Connection Con ;
    private MySingleTon() {
    	System.out.println("Hello");
    	Con= createConnection();
    }
    
    @SuppressWarnings("rawtypes")
    public Connection createConnection() {
    	Connection connection = null;
    	try {
    		// Load the JDBC driver
    		Class driver_class = Class.forName(driver);
    		Driver driver = (Driver) driver_class.newInstance();
    		DriverManager.registerDriver(driver);
    		connection = DriverManager.getConnection(url + dbName);
    	} catch (ClassNotFoundException e) {
    		e.printStackTrace();
    	} catch (SQLException e) {
    		e.printStackTrace();
    	} catch (IllegalAccessException e) {
    		e.printStackTrace();
    	} catch (InstantiationException e) {
    		e.printStackTrace();
    	}
    	return connection;
    }
    
    /**
     * Create a static method to get instance.
     */
    public static MySingleTon getInstance() {
    	if (myObj == null) {
    		myObj = new MySingleTon();
    	}
    	return myObj;
    }
    
    public static void main(String a[]) {
    	MySingleTon st = MySingleTon.getInstance();
    }
}

I am new to java. Please help.

Java Solutions


Solution 1 - Java

It seems the mysql connectivity library is not included in the project. Solve the problem following one of the proposed solutions:

  • MAVEN PROJECTS SOLUTION

Add the mysql-connector dependency to the pom.xml project file:

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

Here you are all the versions: https://mvnrepository.com/artifact/mysql/mysql-connector-java

  • ALL PROJECTS SOLUTION

Add the jar library manually to the project.

Right Click the project -- > build path -- > configure build path

In Libraries Tab press Add External Jar and Select your jar.

You can find zip for mysql-connector here

  • Explanation:

When building the project, java throws you an exception because a file (the com.mysql.jdbc.Driver class) from the mysql connectivity library is not found. The solution is adding the library to the project, and java will find the com.mysql.jdbc.Driver

Solution 2 - Java

If you got the error in your IDE(compile-time error), you need to add your mysql-connector jar file to your libs and add this to your referenced library of project too.

If you get this error when you are running it, then probably its because you have not included mysql-connector JAR file to your webserver's lib folder.

Add mysql-connector-java-5.1.25-bin.jar to your classpath and also to your webserver's lib directory. Tomcat lib path is given as an example Tomcat 6.0\lib

Solution 3 - Java

Every one has written an answer but I am still surprised that nobody actually answered it by using the best simple way.
The people answer that include the jar file. But, the error will still occur.

> The reason for that is, the jar is not deployed when the project is run. So, what we need to do is, tell the IDE to deploy this jar also.

The people here has answered so many times that put that jar file in the lib folder of WEB-INF. That seems okay, but why do it manually. There is simple way. Check the below steps:

> Step 1: If you haven't referenced the jar file into the project then, reference it like this. > > Right click on the project and go to the project properties. Then, go to the java build path, then add external jar file via that.

But this will still not solve the problem because adding the external jar via build path only helps in compiling the classes, and the jar will not be deployed when you run the project. For that follow this step

> Right click on the project and go to the project properties. Then, go to the Deployment Assembly then press Add , then go to the java build path entries and add your libraries whether it is jstl, mysql or any other jar file. add them to deployment. Below are the two pictures which display it.

Before Adding

After Adding

Solution 4 - Java

For Gradle-based projects you need a dependency on MySQL Java Connector:

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

Solution 5 - Java

You will have to include driver jar for MySQL MySQL Connector Jar in your classpath.

If you are using Eclipse: How to add dependent libraries in Eclipse

If you are using command line include the path to the driver jar using the -cp parameter of java.

java -cp C:\lib\* Main

Solution 6 - Java

check for jar(mysql-connector-java-bin) in your classpath download from here

Solution 7 - Java

JDBC API mostly consists of interfaces which work independently of any database. A database specific driver is required for each database which implements the JDBC API.

First download the MySQL connector jar from www.mysql.com, then:

Right Click the project -- > build path -- > configure build path

In the libraries tab press Add External Jar and select your jar.

Solution 8 - Java

For Maven based projects you need a dependency.

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

Solution 9 - Java

In the project into the folder Libraries-->right click --> Add Library --> Mysqlconnector 5.1

Solution 10 - Java

The driver connector is not in your build path. Configure the build path and point it to the 'mysql-connector-java-5.1.25-bin.jar' (check the version which you are using). Alternatively you can use maven :D

Solution 11 - Java

For IntelliJ Idea, go to your project structure (File, Project Structure), and add the mysql connector .jar file to your global library. Once there, right click on it and chose 'Add to Modules'. Hit Apply / OK and you should be good to go.

Solution 12 - Java

This needs to be used as of 2021

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

Solution 13 - Java

Trivial as it may seem in my case netbeans version maven project 7.2.1 was different. There is a folder in the project called dependencies. Right click and then it brings up a popup window where you can search for packages. In the query area put

mysql-connector

It will bring up the matches (it seems it does this against some repository). Double click then install.

Solution 14 - Java

It is because the WEB-INF folder does not exist at the location in the sub directory in the error. You either compile the application to use the WEB-INF folder under public_html OR copy the WEB-INF folder in sub folder as in the error above.

Solution 15 - Java

The exception can also occur because of the class path not being defined.

After hours of research and literally going through hundreds of pages, the problem was that the class path of the library was not defined.

Set the class path as follows in your windows machine

set classpath=path\to\your\jdbc\jar\file;.

Solution 16 - Java

I Understood your problem add this dependency in your pom.xml your problem will be solved,

https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.38

Solution 17 - Java

If you are using tomcat then along with project directory you should also copy the database connector jar file to tomcat/lib. this worked for me

Solution 18 - Java

I'm developing a simple JavaFX11 application with SQLite Database in Eclipse IDE. To generate report I added Jasper jars. Suddenly it throws "THIS" error.

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

It was running good (BEFORE THIS ADDITION). But suddenly!

I'm not using maven or other managers for this simple application. I'm adding jars manually.

I created "User Library" and added my jars from external folders.

PROBLEM OCCURING AREA: My "User Library" are marked as system library. I just removed the marking. Now its not a system library. "NOW MY PROJECT WORKING GOOD".

DEBUG MYSELF: Tried other things: AT RUN CONFIGURATION: Try, removing library and add jars one-by-one and see. - here you have to delete all jars one by one, there is no select all and remove in eclipse right now in run configuration. So the error messages changes form one jars to another.

Hope this helps someone.

Solution 19 - Java

I was also facing the same problem

> Download mysql-connector-java jar file > > paste it in the C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib > > Hope this work for you also !!

Solution 20 - Java

Finally

I solved by two steps :

1 - add the below to pom.xml

  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.8</version>
        </dependency>

2 - Download jar file from this URL:https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.0.8 after that put it in your tomcat/lib folder.

Solution 21 - Java

I was having the same issue. I was using intellijj IDE for creating the MySQL connection.

Steps to fix it:

  1. Download: mysql-connector.jar( I used 8.0.29).

  2. Go to "file-->project structure -->Libraries-->Click on plus button and select java and select the jar file you downloaded in step 1". enter image description here

  3. Check the jar file is showing under "External Libraries directory"

enter image description here 4. Now try to create the connection. It will work.

I used this code for creating MySQL connection:

void createConnection() throws SQLException, ClassNotFoundException {
    Connection connection=null;
    try {

        Class.forName("com.mysql.cj.jdbc.Driver");
          connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/candelete"
                ,"root","");
        System.out.println("Connection created");
        System.out.println("hashcode is: "+connection.hashCode());
    }
    finally {
        System.out.println("here");
        System.out.println("hashcode is: "+connection.hashCode());
        connection.close();
        System.out.println("hashcode now: "+connection.hashCode());
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionatikul hussainView Question on Stackoverflow
Solution 1 - JavaR9JView Answer on Stackoverflow
Solution 2 - JavaAsadView Answer on Stackoverflow
Solution 3 - JavaTahir Hussain MirView Answer on Stackoverflow
Solution 4 - JavanaXa stands with UkraineView Answer on Stackoverflow
Solution 5 - JavaNarendra PathaiView Answer on Stackoverflow
Solution 6 - JavaKhAn SaAbView Answer on Stackoverflow
Solution 7 - JavaSathish SRView Answer on Stackoverflow
Solution 8 - JavatomtomssiView Answer on Stackoverflow
Solution 9 - JavaKika_FortezView Answer on Stackoverflow
Solution 10 - JavashikjohariView Answer on Stackoverflow
Solution 11 - JavaMichael SimsView Answer on Stackoverflow
Solution 12 - Javalondon_utkuView Answer on Stackoverflow
Solution 13 - JavaPaulView Answer on Stackoverflow
Solution 14 - JavaMilesWebView Answer on Stackoverflow
Solution 15 - JavaHarshView Answer on Stackoverflow
Solution 16 - JavaGenView Answer on Stackoverflow
Solution 17 - JavaSantosh KadamView Answer on Stackoverflow
Solution 18 - JavaSudhakar KrishnanView Answer on Stackoverflow
Solution 19 - JavaSuraj VermaView Answer on Stackoverflow
Solution 20 - JavaAbd AbughazalehView Answer on Stackoverflow
Solution 21 - JavaManinderView Answer on Stackoverflow