ClassNotFoundException when connecting to Mysql with JDBC

Posted by Jacob Lyles on Stack Overflow See other posts from Stack Overflow or by Jacob Lyles
Published on 2010-03-26T19:18:34Z Indexed on 2010/03/26 19:23 UTC
Read the original article Hit count: 420

Filed under:
|
|

I'm getting the following error when I try to run a simple Java JDBC program at the command line:

Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver/java
Caused by: java.lang.ClassNotFoundException: LoadDriver.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)

Here's the simple Java program, copied right out of the JDBC docs:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
public class LoadDriver {
    public static void main(String[] args) {
        try {
            // The newInstance() call is a work around for some
            // broken Java implementations
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
        throw ex;
            // handle the error
        }
    }
}

Problem is, I'm bloody sure my bash shell $ClASSPATH variable is pointed at the correct .jar file. To be sure, I copied the JDBC .jar to the same directory as my program and ran it as follows:

java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java 

I still get the same error.

I'm running under Mac OSX, if it matters.

© Stack Overflow or respective owner

Related posts about java

Related posts about jdbc