简体   繁体   中英

ClassNotFoundException while deserializing in JAVA

While reading about the Serialization and DeSerialization in JAVA, I came across the below concept:

When you deserialize a serialized object tree, the classes of all the objects have to be on the classpath

Does this mean that if I am the sender and I am sending the serialized object to a receiver, do I need to send the class file of my instance to Receiver as well, so that it can deserialized?

"do I need to send the class file of my instance to Receiver"

No, the receiver must already have exactly that same class (on the classpath). If the receiver does not have your class already (or if the receiver has the wrong version), then the receiver cannot deserialize into your class.

This is why we now use JSON/XML to transfer data. The fields of most Java classes can be serialized into JSON or XML. To transfer algorithms/functions we use versioned Jar file and usually a dependency management tool such as Maven or Gradle. The modern strategy is to separate code and data and transfer them separately.

Serializing and transferring Java objects is a hard problem with hidden pitfalls which make it difficult for beginners, and which is why that strategy is simply avoided by the experienced. This problem has been addressed with Java RPC and CORBA, but both of those are generally eschewed in modern (the last decade) programming, in part because they are very complicated ("heavyweight"), often requiring teams of engineers to implement properly.

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