• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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 java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown 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(DbConnect.java:15)

The command below is succeeding:
> javap com.mysql.jdbc.Driver
Compiled from "Driver.java"
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)
{
try
{
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
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raj Chila
Ranch Hand
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the same error, but I 'm using Netbeans.
Netbeans set classpath itself. Do I need to set it even?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic