This discussion is archived
3 Replies Latest reply: Oct 18, 2012 4:27 AM by gimbal2 RSS

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

969201 Newbie
Currently Being Moderated
Hi,

I know this is a really common problem and I have read a zillion solutions on various forums but I still can't get mine to work!! Hopefully somebody can help?

I am fairly new to Java and am in the learning process. I am using JDK1.7.0_09 on a Windows 7 operating system.

I have down loaded the mysql-connector-java-5.1.22-bin.jar file from Mysql and placed it in various diffent directories and set the CLASSPATH accordingly, but no matter what I do I still get the same error!4

Here are the details and settings as I have them at the moment:

jdk1.7_09 is installed in - c:\program files\java and I have set the PATH to:

c:\program files\java\jdk1.7.0_09\bin\

(This seems to be working OK, I can compile and run various simple programs without any problems)

The mysql-connector-java-5.1.22-bin.jar file is sitting in many places at the moment one of which is in the jdk1.7.0_09\lib\ directory. So I have set the CLASSPATH to:

SET CLASSPATH=%CLASSPATH%; C:\program files\java\jdk1.7.0_09\lib\mysql-connector-java-5.1.22-bin.jar

Here is the file that compiles OK: (Just a downloaded test)


import java.sql.*;
import javax.sql.*;

public class dbdemo{

public static void main (String[] args)
{
Connection conn = null;

try
{

String url = "jdbc:mysql://***.***.**.***/*****";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url,"root"," ");
System.out.println ("Database connection established");
}
catch (Exception e)
{
e.printStackTrace();

}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}

And here is the result (EVERYTIME)!!

C:\Users\Public>java dbdemo
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
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.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at dbdemo.main(dbdemo.java:19)


I would be very grateful if anyone can spot my mistake.

Many thanks
  • 1. Re: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    Tolls Journeyer
    Currently Being Moderated
    You shouldn't be putting things in the JDK lib directory.

    I would suggest (just for simplicities sake) putting the driver jar file in the same directory you are trying to execute the code from (looks like Users/Public).
    Then try:
    java -cp .;mysql-connector-java-5.1.22-bin.jar dbdemo

    This will ensure that the JVM is not pulling in a classpath from somewhere else.
  • 2. Re: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    969201 Newbie
    Currently Being Moderated
    Thank you very much, That's done the trick!

    I did try putting the .jar file in this directory before and then changed the CLASSPATH accordingly but this didn't work.

    Any idea why changing the CLASSPATH does not work but using the -cp option does?
  • 3. Re: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    gimbal2 Guru
    Currently Being Moderated
    966198 wrote:
    Thank you very much, That's done the trick!

    I did try putting the .jar file in this directory before and then changed the CLASSPATH accordingly but this didn't work.

    Any idea why changing the CLASSPATH does not work but using the -cp option does?
    Because you did something wrong - impossible to say what when you don't post the classpath you were using. Note that changes to environmental variables are not active in a running command prompt, you would have to open a new command prompt before the changed classpath is active.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points