File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error" Watch "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error" New topic

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

Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142

I am getting a "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" runtime error while trying to use jdbc to connect to a MySql server on windows. Below is the error. The strange thing is, when I issue the javap command as "javap com.mysql.jdbc.Driver" it does find the class and provides all the details as shown further below. FYI I have also placed mysql-connector-java-5.1.10-bin.jar in my classpath.

Here is the runtime failure
> java DbConnect
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at$ Source)
at Method)
at Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at net.sf.karproject.DogDb.DbConnect.main(

The command below is succeeding:
> javap com.mysql.jdbc.Driver
Compiled from ""
public class com.mysql.jdbc.Driver extends com.mysql.jdbc.NonRegisteringDriver implements java.sql.Driver{
public com.mysql.jdbc.Driver() throws java.sql.SQLException;
static {};

Given the above command works (which means com.msql.jdbc.Driver is in the classpath) why is my attempt to connect to the MySql database using jdbd in java failing? Here is the snippet of the code I am using in my java code to try to connect to the MySql DB:

public static void main(String[] args)
Statement stmt;
Class.forName("com.mysql.jdbc.Driver"); //This is where it is failing during runtime
String url = "jdbc:mysql://localhost:3306/mysql";

I have wasted so much of time trying to debug this with no success . What am I doing wrong? Hope the experts over here will help me figure out the issue here. Thanks a lot.
Peter Johnson

Joined: May 14, 2008
Posts: 5852

How are you running your app? Are you providing a -classpath parameter to java? If so, java ignores CLASSPATH.

By the way, you should never set CLASSPATH, that is a good way to get into trouble when suddenly an app uses an unexpected class. Instead you should always use the -classpath parameter providing the JAR files required by your app.

JBoss In Action
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

It is really strange! and it should not happen...

One thing I find fishy from your post is while your DBConnect class is packaged (net.sf.karproject.DogDb), your invocation looks is java DbConnect (without the package prefix and that is not possible)...Can that hold any clues?

Infact it looks like this is a Duplicate class, that is in the Classpath somewhere... this reiterates Peters comment about having a default CLASSPATH.

Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
Thanks for the response/help.

But never mind, it is my stupid/silly ignorance on not knowing how to setup classpath properly. When I told in my original post "FYI I have also placed mysql-connector-java-5.1.10-bin.jar in my classpath", I meant I put the parent directory containing mysql-connector-java-5.1.10-bin.jar in my classpath. I didn't realize that I have to include the mysql-connector-java-5.1.10-bin.jar file explicitly in the classpath. Once I fixed this, it started working like magic. This was a lesson learnt for me on how to setup classpath when you have a jar file.

Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

Good to know it worked!

But still I am curious to know how your javap command loaded the MySQL Driver if your classpath was not set at all? and also how you were able to invoke DbConnect class without the package prefix and this class gets executed?

sudeep d nayak

Joined: Sep 24, 2012
Posts: 1
I got the same error, but I 'm using Netbeans.
Netbeans set classpath itself. Do I need to set it even?
I agree. Here's the link:
subject: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error
jQuery in Action, 3rd edition