简体   繁体   English

异常-java.lang.NoClassDefFoundError

[英]Exception - java.lang.NoClassDefFoundError

So, i done search for this question.. My directories and files structure looks like (all *.java files have server package): 因此,我完成了对这个问题的搜索。我的目录和文件结构看起来像(所有* .java文件都有server包):

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

run2 contains: 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

As you see, it runs Server . 如您所见,它运行Server Go look there: 去那里看看:

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();

}

}

Nothing weird in this class, right? 这堂课没什么奇怪的吧? Anyway, i think like that. 无论如何,我是这样想的。 In this class, as we see, Server create instance of ServerConnectionManager and call some functions. 如我们所见,在此类中, Server创建ServerConnectionManager实例并调用一些函数。 Go look at acceptConnections : 去看看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");
            }
        }

    }

I commented some lines. 我评论了一些话。 I really not need them now. 我现在真的不需要它们了。

More about problem: 有关问题的更多信息:

When i run run2 - server runs and works fine. 当我运行run2服务器运行正常。 netstat shows what server wait for connections. netstat显示什么服务器等待连接。

But when i run client, and try connect to server, it shows to me next error: 但是,当我运行客户端并尝试连接到服务器时,它向我显示了下一个错误:

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

I can't understand, why i have this exception ? 我不明白,为什么我有这个exception Look at directories and files. 查看目录和文件。 ClientThread.java exists and placed into server directory and have server package. ClientThread.java存在并放置在server目录中并具有server软件包。 Compilation doesn't show any error. 编译没有显示任何错误。

What i doing wrong? 我做错了什么?

There is connection class for client 客户端有连接类

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");
        }
    }
}

在此处输入图片说明

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();

    }


}

You need to compile ClientThread.java as well 您还需要编译ClientThread.java

add this before running as well 还要在运行之前添加它

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

This is painful way of managing classpath and compilation, better go for some IDE and build tool (maven) 这是管理类路径和编译的痛苦方法,最好使用一些IDE和构建工具(Maven)

Server.java does not import ClientThread.java or use ClientThread at all inside the Server class, so your compiler ignores compiling it. Server.java不会在Server类内部完全导入ClientThread.java或使用ClientThread,因此您的编译器将忽略对其进行编译。

In order to fix this problem, simply include ClientThread.java in the batch you use to compile your project. 为了解决此问题,只需在用于编译项目的批处理中包含ClientThread.java

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

I suggest you look into an IDE though 我建议您尽管考虑一下IDE

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

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