简体   繁体   中英

Receiving user defined Object over TCP with ClassNotFoundException

I'm developing a client/server application that uses TCP connection. I need to send a user defined object from server to client. everything is working fine until the client receives the object, it throws the ClassNotFoundException even though the class is on both sides and I cast it, and it is Serializable.

The exception at client side is:

     Exception in thread "main" java.lang.ClassNotFoundException: jggserver.Question

I don't know why the client is casting using the server package. (they are different projects).

here is the code at client side, where the exception is:

    q = (Question) clientInputStream.readObject();

code at server side:

    serverOutputStream.writeObject(question);

Although I went through several tutorials and they do the exact thing.

I appreciate any help.


COMMENTS:

user1697575, both programs are on the same computer and same IDE (netbeans).

  • I don't know about the TCP payload, I will search about it.

  • I create the class on the server then refactor it on the client, I don't know if it's the correct way or not.

  • I can't use RMI because I'm asked to specifically use this way to implement my project.

many thanks.


SOLVED: I created a separate package and added Question class to it, then I included this package in the server and the client programs.

thank you all guys, specially Juan.

I think the answer is in your question, here "I don't know why the client is casting using the server package. (they are different projects)." .

If packages on server and client are different, is impossible that standard serialisation system works.

You need create a class that is present is both projects with same name and package, really "same code".

There are many check points to consider:

  • JVM version on client and server
  • TCP bytes payload check to see if they aren't get corrupted/translated
  • Class versions loaded on the client and server

Also out of curiosity, why don't you use a robust RMI standard framework that taking care of many things for you?

The class is not present on both sides. That's what the exception is telling you. It has to be the same class name, in the same package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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