0

So, i done search for this question.. My directories and files structure looks like (all *.java files have server package):

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

run2 contains:

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. 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. Go look at 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. netstat shows what server wait for connections.

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 ? Look at directories and files. ClientThread.java exists and placed into server directory and have server package. 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");
        }
    }
}

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

    }


}
5
  • 1
    Understand that NoClassDefFoundError is a "garbage" exception (many different causes), and the actual error may not even be in the named class (but rather some class it references). Commented Jul 11, 2014 at 3:02
  • I too think like that. But i have not enough knowledges in java (newbie)... how i can get more detailed information about this error? Commented Jul 11, 2014 at 3:06
  • 1
    Usually the problem is due to mismatched jar files -- eg, abc.jar references version 6 of some 3rd party API while xyz.jar references version 7. But there are many possible variations on that theme, and several others besides. One that can really confuse is having the wrong package name in a .java file you compile, or simply having class Abc in the file Abcd.java. Commented Jul 11, 2014 at 3:19
  • Hmm... I go check for typo mistakes Commented Jul 11, 2014 at 3:21
  • @HotLicks , you are right. Error in ClientThread class or other. Error in shell script :( When i removed next line, all works fine - find . -name "*.class" -type f -delete . Thankyou, your answers so userfull. Please, order your comments as anwer and i mark it as accepted Commented Jul 11, 2014 at 3:36

2 Answers 2

2

You need to compile ClientThread.java as well

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)

Sign up to request clarification or add additional context in comments.

7 Comments

can you see class file placed at from your current directory ./server/ClientThread.class
Yes, file exists in ./server dir
can you post your directory structure including .java and .class, along with new error message (on your question)
Okay, a few seconds, please
and it is still failing to find ClientThread.class ? if yes post your source for that class
|
1

Server.java does not import ClientThread.java or use ClientThread at all inside the Server class, so your compiler ignores compiling it.

In order to fix this problem, simply include ClientThread.java in the batch you use to compile your project.

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

I suggest you look into an IDE though

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.