mysql JDBC连接NoClassDefFoundError

时间:2014-11-22 03:10:37

标签: java mysql jdbc runtime-error

我正在尝试在Windows 2008服务器上测试mySQL JDBC连接。我已经下载了JDBC驱动程序,它在" C:\ Program Files \ MySQL \ MySQL Connector J \ mysql-connector-java-5.1.34-bin.jar"中创建了一个jar文件。 当我运行一个小程序来测试我的jdbc连接时,我得到了#34; NoClassDefFound"错误。 我在这里缺少什么?

我在classpath

中设置了jdbc jar
C:\>echo %CLASSPATH%
C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;

我把jar放在我有DBDemo.java的相同位置(C:\ test)

DemoDB.java

import java.sql.*;
import java.util.Properties;
public class DBDemo
{
// The JDBC Connector Class.
private static final String dbClassName = "com.mysql.jdbc.Driver";
// Connection string. emotherearth is the database the program
// is connecting to. You can include user and password after this
// by adding (say) ?user=paulr&password=paulr. Not recommended!
private static final String CONNECTION =
                      "jdbc:mysql://127.0.0.1/emotherearth";
public static void main(String[] args) throws
                         ClassNotFoundException,SQLException
{
    System.out.println(dbClassName);
    // Class.forName(xxx) loads the jdbc classes and
    // creates a drivermanager class factory
    Class.forName(dbClassName);
    // Properties for user and password. Here the user and password are both 'paulr'
    Properties p = new Properties();
    p.put("user","paulr");
    p.put("password","paulr");
    // Now try to connect
    Connection c = DriverManager.getConnection(CONNECTION,p);
    System.out.println("It works !");
    c.close();
}
}

*错误*

C:\Program Files\Java\jdk1.6.0_45\bin>java c:\test\DBDemo
Exception in thread "main" java.lang.NoClassDefFoundError: c:\test\DBDemo
Caused by: java.lang.ClassNotFoundException: c:\test\DBDemo
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
 Could not find the main class: c:\test\DBDemo.  Program will exit.

3 个答案:

答案 0 :(得分:1)

C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;

您的CLASSPATH似乎没有.,这是DBDemo所在的当前工作目录。因此,.也添加CLASSPATH。之后,您的CLASSPATH就像

.;C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;

修改

同时尝试将mysql-connector-java-5.1.34-bin.jar的内容提取到文件夹中,然后根据该内容更改CLASSPATH

答案 1 :(得分:1)

线程“main”中的异常java.lang.NoClassDefFoundError

java尝试查找.class文件的地方之一是您当前的目录。因此,如果您的.class文件位于c:\test\,则应将当前目录更改为该目录。

要更改目录,请在提示符处键入以下命令,然后按Enter键:

cd c:\test\

另外

set CLASSPATH = .;C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;C:\Program Files\Java\jdk1.6.0_45\lib\tools.jar;

set PATH = C:\Program Files\Java\jdk1.6.0_45\bin

使用此命令执行程序应该可以解决问题:

c:\test>java DemoDB

答案 2 :(得分:0)

此问题可能有多种原因,但最常见的原因之一是类或依赖项无法互相看到,如果已重命名了程序包,类或依赖项,则必须验证每个类或依赖项的导入,然后再对JVM进行验证。找不到所需的类或依赖项。