tags:

views:

309

answers:

3

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.

Edit:

I followed Powerlord's suggestion below, and now I am still getting virtually the same exception.

I entered:

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

Whether or not I leave the classpath flag on the second command seems not to matter. I am still getting:

Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver
Caused by: java.lang.ClassNotFoundException: LoadDriver
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)
+1  A: 

Short version:
javac requires you to put the .java at the end, java requires you to not put the .java at the end.

As Jim Garrison noted before he deleted his answer, this command-line to run the program is wrong.

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

This tells Java to load LoadDriver/java.class

What you actually want is

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

Provided of course that you compile it first with

javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
R. Bemrose
A: 

The problem is not the missing driver, it's the missing LoadDriver class. You need to compile the .java source file to a .class file first:

javac LoadDriver.java
FRotthowe
+2  A: 

I think your syntax is just wrong here. Have you already compiled LoadDriver.java using:

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

If so, then you should be able to do :

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

(Note that I removed the '.java' from the end)

elduff
This was almost right. I also had to add ./ to my classpath. So the final command line was java -classpath ./:./mysql-<whatever>.jar LoadDriver
Jacob Lyles
Actually, you don't need mysql-connector-java-5.1.12-bin.jar in your classpath when compiling, because the driver is only needed at runtime (it is loaded by reflection).
FRotthowe