I get the following error when I try to run a simple Java JDBC program on 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 a simple Java program copied directly from JDBC docs:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class LoadDriver {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
throw ex;
}
}
}
The problem is that I'm damn sure my bash shell variable $ ClASSPATH is pointed to the correct .jar file. Of course, I copied JDBC.jar to the same directory as my program, and executed it as follows:
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
I still get the same error.
Edit:
I followed the Powerlord suggestion below, and now I'm still getting pretty much the same exception.
I entered:
javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
java LoadDriver
I can not leave the classpath flag in the second command, it does not matter. I still get:
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)
source
share