获取NoClassDefFoundError:com.mysql.jdbc.Driver

时间:2012-05-08 15:00:26

标签: java mysql exception jdbc noclassdeffounderror

我目前正在java中编写一个jdbc客户端 - 服务器套接字应用程序。我在初始化jdbc时遇到了一些麻烦,因为我没有使用ide,只是一个文本编辑器和jdk。我把我的jdbc驱动程序放在我的jdk和jre类路径C:\ Program Files \ Java \ jdk1.6.0_20 \ jre \ lib \ ext和\ jre \ lib \ ext中。我还将jar重命名为com.mysql.jdbc.driver,但我仍然没有运气。这是驱动程序未找到的问题还是其他问题?

这是我的错误:

C:\Users\imallin\My Documents>java Provider
Waiting for connection
Connection received from 127.0.0.1
server>Hi welcome to the Cyfieithu Eadar-Theangachadh translation server, please
enter a command. Type cmd to see a list of commands
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at Provider.insertData(Provider.java:82)
    at Provider.run(Provider.java:40)
    at Provider.main(Provider.java:118)

这是我的代码:

import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Provider{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Connection con;
Provider(){}
void run()
{
        Console c = System.console();
    if (c == null) {
        System.err.println("No console.");
        System.exit(1);
    }
    try{
        //1. creating a server socket
        providerSocket = new ServerSocket(2004, 10);
        //2. Wait for connection
        System.out.println("Waiting for connection");
        connection = providerSocket.accept();
        System.out.println("Connection received from " + connection.getInetAddress().getHostName());
        //3. get Input and Output streams
        out = new ObjectOutputStream(connection.getOutputStream());
        out.flush();
        in = new ObjectInputStream(connection.getInputStream());
                        sendMessage("Hi welcome to the Cyfieithu Eadar-Theangachadh translation server, please enter a command. Type cmd to see a list of commands");
        //4. The two parts communicate via the input and output streams
        do{
            try{
                                    insertData();

            message = (String) in.readObject();
            System.out.println("client>" + message);
                                if (message.equals("register"))
                    sendMessage("first name?");
                    String firstName = (message);
                    sendMessage("first name = " + firstName);
                    insertData();
                                if (message.equals("listlanguages"))
                                sendMessage("English \n Thai \n Geordia \n Gaelic \n Welsh \n Gaelic \n Urdu \n Polish \n Punjabi \n Cantonese \n Mandarin");
                    if (message.equals("bye"))
                    sendMessage("bye");
                    }
            catch(ClassNotFoundException classnot){
                System.err.println("Data received in unknown format");
            }
            catch (Exception e){
            System.err.println("Error: " + e.getMessage());
            }
        }while(!message.equals("bye"));
    }
    catch(IOException ioException){
        ioException.printStackTrace();
    }
    finally{
        //4: Closing connection
        try{
            in.close();
            out.close();
            providerSocket.close();
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
    }
}
    private void insertData()
{

    try
    {
        Class.forName("com.mysql.jdbc.Driver");


        con = DriverManager.getConnection(
                "jdbc:mysql://localhost/translator","root","");

String sql = "Insert INTO users (ID, firstName) VALUES ('123','123')";

        Statement statement = con.createStatement();

        statement.execute(sql);


    }
                    catch (Exception e){
            System.err.println("Error: " + e.getMessage());
            }
    }


void sendMessage(String msg)
{
    try{
        out.writeObject(msg);
        out.flush();
        System.out.println("server>" + msg);
    }
    catch(IOException ioException){
        ioException.printStackTrace();
    }
}

public static void main(String args[])
{
    Provider server = new Provider();
    while(true){
        server.run();
    }
}
}

3 个答案:

答案 0 :(得分:1)

最终没有设置jdbc的类路径。

这就是我所做的(一种不好的做法)

提取你的jar文件。 (假设您在主目录中工作〜,并且提取jar文件的目录名称为odbc) 然后将类路径设置为

export CLASSPATH=~/odbc:

(不要忘记在路径后添加: ;-))

哦,无法意识到你正在使用Windows机器。 然后 设置环境变量(如果不存在则创建)CLASSPATH到c:\odbcdir

或其他方式是使用java -cp pathToJAR.jar Main

答案 1 :(得分:1)

就像已经暗示的其他答案一样,jdbc Jar不在你的类路径上。

要解决此问题,您需要在运行Java主文件时声明它应该使用该Jar,方法是:

java -cp "nameOfJar.jar" Provider

这假设你的jar与你的提供者文件在同一目录上,如果不是,你应该指定它的路径。

这完全是可选的,但是我喜欢有一个名为“lib”的目录,我在其中放置了运行项目所需的所有jar,然后你可以通过这样做添加到classpath:

java -cp "./lib/*;." NameOfJavaClassWithMain

最终,您可能最终需要添加到类路径中的多个目录或jar,通过使用;(Linux中的:)分隔符来完成此操作,例如:

java -cp "./lib/*;./conf/configurationFile.properties;." NameOfJavaClassWithMain

如果你的.class不在其中一个Jars上,你可能需要将本地目录添加到类路径中:

java -cp "./lib/*;." NameOfJavaClassWithMain

如果您的类上有一个包声明(您应该),则需要通过完全分类的名称(包+ ClassName)引用该类,如下所示:

java -cp "./lib/*;." my.package.NameOfJavaClassWithMain

答案 2 :(得分:0)

MySQL Driver jar不在类路径上。您需要使用-cp的{​​{1}}参数来解决此问题。

相关问题