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

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

If the jar is already added to your external libs, you can simply add :

import com.mysql.jdbc.Driver;

and it shall work in your class.

This shall help further SO-21580499


Some of the most possible reasons of "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" in your code are:

  1. You don't have mysql-connector.jar in your Classpath.
  2. mysql-connector.jar is in your classpath but somehow your classpath is getting overridden.
  3. mysql-connector.jar is in classpath but current user doesn't have read permission.