Muhammad Faizan Ul Haq Muhammad Faizan Ul Haq - 2 years ago 246
SQL Question

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error even after importing library

I have imported my library "mysql-connector-java-5.1.39"
as answered by most people to exact same question ,
but I am still getting this error

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

here is the code of test class

package database;

import java.sql.Connection;
import java.sql.DriverManager;

public class Main {

public static void main(String[] args) throws Exception {

public static Connection getConnection() throws Exception {
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String username="root";
String password="root";
Connection conn= DriverManager.getConnection(url,username,password);
return conn;
catch (Exception e){
return null;

eclipse directory

Using mac OS .

Answer Source

Everybody repeat after me. ( :-) )

"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" is NOT a compilation error.

Therefore, changing the >>build<< path or adding an import cannot fix the problem.

The solution is to make sure that the JAR file is on the classpath when you run the application. For example, if your test class is in bin\database\Main.class and the driver JAR is in lib ...

$ java -classpath bin:lib/mysql-connector-java-5.1.39.jar database.Main
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download