This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem with Serialization / Deserialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with Serialization / Deserialization" Watch "Problem with Serialization / Deserialization" New topic

Problem with Serialization / Deserialization

Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456

I have a problem with my first steps in Serialization / Deserialization.

Scenarion 1:
- Create a HashSet
- fill it with Integer objects
- serialize it to file
-> ok.
- deserialize it from file
- read HashMap
-> ok.

Scenarion 2, almost the same code
- Create a HashSet,
- fill it with trivial objects ("SerObject", only one String member)
- serialize it to file
- deserialize it from file
-> ClassNotFoundException SerObject.

can someone please point me in the right direction?

many thanks,
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

This sounds like a common misconception about what serialization does: the SerObject class file has to be available on the classpath of the program that deserializes the file. The class definition isn't serialized, only the objects themselves.

[Jess in Action][AskingGoodQuestions]
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
Hello Ernest,

thanks for your answer. I forgot to mention that I've put the SerObject's class definition into the deserialization client's package. (Serializer and Deserializer are running on different VMs)

So that's probably not causing the fault...

Philip Heller
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
How did you put the SerObject's class definition into the deserialization client's package? Did you change the "package" declaration in on the client side? In that case, you have two different classes: serializer.SerObject and deserializer.SerObject. Or did you copy the class file for serializer.SerObject into the package dir for deserializer? In that case, it should be in a "serializer" directory instead to match its package hierarchy.

If it's neither of those, it's something more insidious.

Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
Phillip, you were 100% right... I took the class definition over to the client, but put it into the wrong package.... STupid me.

Thx a lot for helping,
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Problem with Serialization / Deserialization
Similar Threads
Writing/Reading multiple objects of same class using Serialization?
ObjectInputStream.available() returning 0, but readObject returns the object
Serialization in java
Why are transient static variables allowed? invalid stream header:i understand its a thread issue not sure fix