0

I want to make chat application using java networking. So far I have made server and client class in two different projects and each of them have the same Message class:

package client;

import java.io.Serializable;

public class Message implements Serializable {

public static final long serialVersionUID = 1l;

private String text;
private int receiversID;
private int senderID;

public Message(String text, int receiversID, int senderID){
    this.text = text;
    this.receiversID = receiversID;
    this.senderID = senderID;
}

public String getText() {
    return text;
}

public int getReceiversID() {
    return receiversID;
}

public int getSenderID() {
    return senderID;
}

Than I send a message inside client class like this:

            do {
            System.out.print("Enter message to be sent: ");
            text = scanner.nextLine();
            System.out.print("Enter receivers ID: ");
            receiverID = scanner.nextInt();

            outputStream.writeObject(new Message(text, receiverID, ID)); // sending message
            outputStream.flush();

            if (!text.equals("exit")) {
                try {
                    Message message = (Message) inputStream.readObject();
                    System.out.print(message.getText() + "\n");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }
        } while (!text.equals("exit"));

The error occurs when I want to get that message in my server project:

        Message message = (Message) inputStream.readObject();

And I get this:

java.lang.ClassNotFoundException: client.Message
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:683)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1863)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1746)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2037)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1568)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:428)
at network.ClientSockets.run(ClientSockets.java:57)

I know that server project can not recognize Message class from client despite they are the same classes, but I really don't know how to fix this.

Does anyone know the solution?

  • 1
    The error says `at network.Client.run(Client.java:55)`. Is the error on the server or the client? – Steve Smith Aug 16 '18 at 13:14
  • its just a name, its not client class from another project, that class contains socket of client in server project – Jovan Dukic Aug 16 '18 at 13:17
  • can you post line number 55 of Client (or rather check yourself more line 55 of Client) – nits.kk Aug 16 '18 at 13:18
  • Message message = (Message) inputStream.readObject(); – Jovan Dukic Aug 16 '18 at 13:19
  • hmm also check the package name of Message class at both Server and Client end – nits.kk Aug 16 '18 at 13:19
  • I have made client class too inside server project which contains sockets from clients, and that I put clients inside an array – Jovan Dukic Aug 16 '18 at 13:20
  • you can try debugging : inputStream.readObject().getClass() It will let you know the actual class – nits.kk Aug 16 '18 at 13:20
  • To clarify, you have two `Message.java`'s, one in the client project and one in the server project? Do they both have the same package name? The error says that the server is looking for `client.Message`. – Steve Smith Aug 16 '18 at 13:22
  • Nope, they don't have the same package name – Jovan Dukic Aug 16 '18 at 13:23
  • Try giving them the same package name. – Steve Smith Aug 16 '18 at 13:24
  • there you go!! you have two ends. End1 sends X.A to End2 and End2 has Y.A so tries to decode the recieved data and fails to recognize the data as it does not have X.A. – nits.kk Aug 16 '18 at 13:25
  • That is true, but still what shoud I do? – Jovan Dukic Aug 16 '18 at 13:27
  • also this link may be helpful https://stackoverflow.com/a/7050688/504133 I am not sure as I do not have PC to verify it now but may be you will have to implement Serializable interface and have same Serial version id at both client and server end for Message class – nits.kk Aug 16 '18 at 13:28
  • @nits.kk They've already done that. – Steve Smith Aug 16 '18 at 13:29
  • Ideally the client and server should share the same source file. Other than that, just try changing the package names to make them the same (`package client;` -> `package shared;` for example). – Steve Smith Aug 16 '18 at 13:30
  • what shall i do ? My suggestion .. Same class name, keep it in same named package, implement Serializable interface and have same serial verdion id. I.e. have similar copies of Message class at both end Server as well as Client side. Do not make Message as inner class. *Keep it in same named package as a separate outer public class in its own java file* – nits.kk Aug 16 '18 at 13:30
  • 1
    It works!!! They all just have to have the same package names thanks you all !!! – Jovan Dukic Aug 16 '18 at 13:33

0 Answers0