简体   繁体   English

使用JDBC连接到Mysql时出现ClassNotFoundException

[英]ClassNotFoundException when connecting to Mysql with JDBC

I'm getting the following error when I try to run a simple Java JDBC program at the command line: 当我尝试在命令行运行一个简单的Java JDBC程序时,我收到以下错误:

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: 这是简单的Java程序,直接从JDBC文档中复制出来:

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. 问题是,我很确定我的bash shell $ ClASSPATH变量指向正确的.jar文件。 To be sure, I copied the JDBC .jar to the same directory as my program and ran it as follows: 可以肯定的是,我将JDBC .jar复制到与我的程序相同的目录中,并运行如下:

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. 我按照下面的Powerlord的建议,现在我仍然得到几乎相同的例外。

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. 我是否在第二个命令上留下classpath标志似乎并不重要。 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)

I think your syntax is just wrong here. 我认为你的语法在这里是错误的。 Have you already compiled LoadDriver.java using: 您是否已使用以下方法编译LoadDriver.java:

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) (注意我从末尾删除了'.java')

Short version: 精简版:
javac requires you to put the .java at the end, java requires you to not put the .java at the end. javac要求你把.java放在最后, java要求你不要把.java放在最后。

As Jim Garrison noted before he deleted his answer, this command-line to run the program is wrong. 正如Jim Garrison在删除他的回答之前所指出的那样,运行该程序的命令行是错误的。

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

This tells Java to load LoadDriver/java.class 这告诉Java加载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

The problem is not the missing driver, it's the missing LoadDriver class. 问题不在于缺少的驱动程序,而是缺少的LoadDriver类。 You need to compile the .java source file to a .class file first: 您需要首先将.java源文件编译为.class文件:

javac LoadDriver.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM