ClassNotFoundException when connecting to Mysql with JDBC

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;
// 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
        }
    }
}

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)
+3
source share
3

, . LoadDriver.java, :

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

, :

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

( , ".java" )

+3

:
javac , .java , java , .java .

, , .

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

Java LoadDriver/java.class

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

,

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

The problem is not the missing driver, but the missing LoadDriver class. First you need to compile the source .java file into a .class file:

javac LoadDriver.java
0
source

Source: https://habr.com/ru/post/1738708/


All Articles