• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization Failing with Incompatiable class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

I have an class which implements serializable.i have made some changes and redeployed but its giving me the InvalidClassException.incompatiable Classes and diffrent serialversioUID in the stream class and local class.
I have added the serialversionUID in the log which shown and its working fine.


But..is the same serialVersionUID would works for all envn.because i have tried in my QA and Dev envn..but when it goes to prodn is it works?

what if instead hard coding the serialversionUID to some value shown in the Log... by 1L?

my Sub Class extends the Class which Base Class is Serializable actually,is the need of my subclass holds this value..i founded my base class does nt have the serialversionUID.?

Please help on this.

Thanks,
karthik.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome,

http://faq.javaranch.com/java/TellTheDetails
http://faq.javaranch.com/java/UseRealWords

Show us the code. Copy/paste the actual errors. Then we can help much more easily without us having to guess what's going wrong.
 
Karthikeyan Srinivasan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody who looks..its worked just added by the serialVersionUID in the subclass.and ofcourse i will take care of to post the topic with correct example...
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a class is Serializable, but there is no serialVersionUID field, then Java makes up its own UID for the class. This changes if the fields or methods change. So a tiny change to the class can result in serialisation failure.

If you give a serialVersionUID field in the class, and it is the same as one being read from a stream, Java will attempt to populate the object with the values from the stream. Any fields present in the stream, and missing in the class, will be ignored. Any fields present in the class, and missing in the stream, will be defaulted (normal Java rules apply).

Normally, the actual value of serialVersionUID does not matter all that much. You could use 1 for all classes. However, that would pretty much defeat all the checking, so might not be advisable.

One approach is to find out the UID of the first version of your class, when it doesn't have a serialVersionUID field, set serialVersionUID to that value, and leave it at that value forever. Or, at least, until you make changes that mean that old serialised streams really can't be read.

Another approach (what I usually do) is just to make up a random number in my head, when I first write the class.

If you have to read streams that were written before you added a serialVersionUID field, you should set your serialVersionUID to the value in the stream (assuming there's just one value, else you're stuck).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic