简体   繁体   English

mysql JDBC 连接 NoClassDefFoundError

[英]mysql JDBC Connection NoClassDefFoundError

I am trying to test mySQL JDBC connection on a windows 2008 server.我正在尝试在 Windows 2008 服务器上测试 mySQL JDBC 连接。 I have downloaded the JDBC driver and it created a jar file at "C:\\Program Files\\MySQL\\MySQL Connector J\\mysql-connector-java-5.1.34-bin.jar".我已经下载了 JDBC 驱动程序,它在“C:\\Program Files\\MySQL\\MySQL Connector J\\mysql-connector-java-5.1.34-bin.jar”创建了一个 jar 文件。 When I am running a small program to test my jdbc connection , I am getting "NoClassDefFound" error.当我运行一个小程序来测试我的 jdbc 连接时,我收到“NoClassDefFound”错误。 What am i missing here?我在这里缺少什么?

I did set up jdbc jar in classpath我确实在类路径中设置了 jdbc jar

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

I have placed the jar in same location where I have my DBDemo.java (C:\\test)我已将 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();
}
}

*Error * *错误 *

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.
C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;

Your CLASSPATH seems to be not having .您的CLASSPATH似乎没有. that is the current working directory where the DBDemo would be there.这是DBDemo所在的当前工作目录。 So add .所以添加. also to your CLASSPATH .也到您的CLASSPATH After that your CLASSPATH will be like之后你的CLASSPATH会像

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

EDIT编辑

Also try extracting the contents of mysql-connector-java-5.1.34-bin.jar into a folder and change the CLASSPATH according to that.还尝试将mysql-connector-java-5.1.34-bin.jar的内容提取到一个文件夹中,并根据该文件夹更改CLASSPATH

Exception in thread "main" java.lang.NoClassDefFoundError线程“main”中的异常 java.lang.NoClassDefFoundError

One of the places java tries to find your .class file is your current directory. java 试图找到您的.class文件的地方之一是您的当前目录。 So if your .class file is in c:\\test\\ , you should change your current directory to that.因此,如果您的.class文件在c:\\test\\ ,您应该将当前目录更改为该目录。

To change your directory, type the following command at the prompt and press Enter:要更改目录,请在提示符下键入以下命令并按 Enter:

cd c:\test\

Also

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

Executing your program using this command should correct the problem:使用此命令执行程序应该可以解决问题:

c:\test>java DemoDB

这个问题可能有多种原因,但最常见的一个是类或依赖项无法相互看到,您必须验证每个类或依赖项的导入,如果您重命名了包、类或依赖项,则JVM找不到寻求阶级或依赖。

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

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