异常 - java.lang.NoClassDefFoundError

时间:2014-07-11 02:59:33

标签: java

所以,我搜索了这个问题.. 我的目录和文件结构看起来像(所有* .java文件都有server包):

run2 (shell script)
[server] (folder)
\
 \ Server.java
   ClientThread.java
   ServerConnectionManager.java
   .. some other files ...

run2包含:

find . -name "*.class" -type f -delete
javac -classpath .:server:server/lib/mysqlconn.jar server/Server.java
java -classpath .:server:server/lib/mysqlconn.jar server.Server

如您所见,它会运行Server。去看那里:

package server;

// imports

public class Server {

public static final int BUFFSIZE = 32;

public static void main (String[] args) throws IOException {

    ServerConnectionManager server = new ServerConnectionManager(1234);
    server.start();
    server.acceptConnections();
    server.shutdown();

}

}

这堂课没什么奇怪的吧?无论如何,我想是那样的。 在本课程中,正如我们所见,Server创建了ServerConnectionManager的实例 并调用一些功能。 去看看acceptConnections

public void acceptConnections() {

        while(true) {
            try {
                Socket clientConnection = connection.accept();
                ClientThread client = new ClientThread(clientConnection);
                /*clients.add(client);
                client.start();
                System.out.println("[INF] Client connected");
                System.out.println("[INF] Summary: " + clients.size() + " clients connected");*/
            } catch (IOException e) {
                System.out.println("[ERR] Accepting client connection failed");
            }
        }

    }

我评论了几行。我现在真的不需要它们。

有关问题的更多信息:

当我运行run2时 - 服务器运行并正常运行。 netstat显示服务器等待连接的情况。

但是当我运行客户端并尝试连接到服务器时,它向我显示下一个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: server/ClientThread
    at server.ServerConnectionManager.acceptConnections(ServerConnectionManager.java:36)
    at server.Server.main(Server.java:15)
Caused by: java.lang.ClassNotFoundException: server.ClientThread
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

我无法理解,为什么我有这个exception? 查看目录和文件。 ClientThread.java存在并放入server目录并拥有server个包。编译没有显示任何错误。

我做错了什么?

客户端有连接类

package client;

// imports

public class ClientConnectionManager {

    public Socket   connection;
    public String   host;
    public Integer  port;

    public Boolean  connected;

    public ClientConnectionManager(String h, Integer p) {
        host = h;
        port = p;
    }

    public void connect() {
        try {
            connection = new Socket(host, port);
        } catch (IOException e) {
            System.out.println("[INF] Failed connect to server");
        }
    }

    public void disconnect() {
        try {
            connection.close();
        } catch (IOException e) {
            System.out.println("[ERR] Connection closing failed");
        }
    }
}

enter image description here

package server;

// some imports just not removed yet
import java.io.IOException;
import java.net.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.sql.*;

class ClientThread extends Thread {

    public Socket           clientSocket;

    public OutputStream     out;
    public InputStream      in;

    public Tasks            tasks;
    public User             user;

    public ClientThread(Socket socket) {
        clientSocket = socket;
        try {
            out = clientSocket.getOutputStream();
            in  = clientSocket.getInputStream();
        } catch (IOException e) {
            System.out.println("Cant initialize I/O in for client socket");
        }
    }

    public void run() {

        ServerDatabaseConnectionManager database = new ServerDatabaseConnectionManager();
        database.connect();

        tasks = new Tasks(database.connection);
        user = new User(database.connection);

        /** listen for requests from client*/
        try {
            out.write(new String("_nelo_").getBytes());
        } catch (IOException e) {
            System.out.println("Cant send auth cmd");
        }
        ServerInputHandler listen = new ServerInputHandler(this);
        listen.start();

    }


}

2 个答案:

答案 0 :(得分:2)

您还需要编译ClientThread.java

在运行之前添加它

javac -classpath .:server:server/lib/mysqlconn.jar server/ClientThread.java

这是管理类路径和编译的痛苦方法,最好去一些IDE和构建工具(maven)

答案 1 :(得分:1)

Server.java不会导入ClientThread.java或在Server类中使用ClientThread,因此编译器会忽略编译它。

要解决此问题,只需在用于编译项目的批处理中包含ClientThread.java

javac -classpath .:server:server/lib/mysqlconn.jar server/ClientThread.java

我建议您查看IDE