0

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?

1 Answer 1

3

"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.

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

2 Comments

Theoretically, you could serialized classes as byte streams, send them over the wire, and "load" them from memory on the receiver. Anything is possible with enough of a criminal mindset ;-)
Yes, then you get into the world of Java classloaders. I've written custom classloaders to to exactly that. See "Classloader leak" for a particularly pernicious memory leak. My advice: Use CORBA or RPC, or better yet, just don't do it. :-)

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.