wood burning stoves 2.0*
The moose likes Java in General and the fly likes (De)Serialization question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "(De)Serialization question" Watch "(De)Serialization question" New topic
Author

(De)Serialization question

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

i have an object (that implements Serializable-interface) written to a file some days ago. no problems occured.

today i wanted to read the object back from the file, but the object that's in the file contains some other members of other classes (all implementing Serializable interface) and some of this classes are changed (new members, methods, etc.)

when i try to readObject from file, i got this error:

INFO: Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true
java.io.InvalidClassException: be.aquafin.webgis.kaart.ComplexKaart; local class incompatible: stream classdesc serialVersionUID = -1104219041991736069, local class serialVersionUID = 150420720706126891

so if i save object to file, change some classes, try to get file back, i will always get error. or could i prevent this from happening ?

thanks for your help


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You could add a serialVersionUID to all your Serializable classes. If you haven't defined this then the JVM will try to derive a value for it based on the methods and fields of the Serializable class. So if you've changed/added methods the derived serialVersionUID will be different, but if you define it yourself it will remain unchanged.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Of course, once you explicitly define serialVersionUID, you also have to
take on versioning: Is the new version of your class compatible with
the old, as far as serialization is concerned?
[ October 19, 2005: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

and the serialVersionUID has it to be different for each class, or could i add it to the top of a hierarchical tree and make it protected so all subclasses have the same serial version
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
it's not inherited. Every class should define its own, or
the serialization process will compute a value on the fly.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

and is the compatibility between old version and new version depending on the variables, the methods or both
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
If you define your own serialVersionUID, then its all up to you.
If you are letting the JVM calculate it, well, try the following
experiment...

Run this code to write the file, then again to read it. Then
uncomment method f and run it again.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

i got the point
thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: (De)Serialization question
 
Similar Threads
ArrayList problem ...
Deserialisation when unique ID has changed
re. serialVersionUID
Strange Serialization issue - serialVersionUID need on an interface!?!
Serial Version ID