简体   繁体   中英

ClassNotFoundException in ObjectInputStream.readObject()

I'm trying to cast an object passed by my client application to an object of identical class in my server application.

The error I'm currently receiving in my server application is:

mei 02, 2012 11:44:43 PM server.UserThread process
SEVERE: null
java.lang.ClassNotFoundException: client.User

The object is being received by the server via Socket -> ObjectInputStream.

So I was wondering if you guys could help me cast the client.User class to my Server.User class. The only thing that works is placing the packages inside 1 project and then defining the exact location of the class.

Code can always be supplied.

cast the client.User class to my Server.User class

You can't. They aren't the same class. They are in different packages. They are different. If you want to share a class between server and client, you have to share a single .class file. That might imply creating a third package for shared stuff.

The classes need to be the same, as some users suggest in reply to your question. It doesnt seem like a good choice to use the default serialization procces to flatten and inflate your objects. You can define your own protocol if you use the Externalizable interface, instead of Serializable, you can use writeExternal and readExternal to customize the serialization procces.

If you are trying to send object from server to client, I really discourage the use of the object streams. May I suggest Protobuff to do this?

Protobuff:

Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats.

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