• Post Reply Bookmark Topic Watch Topic
  • New Topic

ObjectInputStream help required in handling the classNotFoundException  RSS feed

 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got to know from the ObjectOutputStream docs, that while writing the object to the file, the default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. I have an issue where the class name has been changed after writing the object to the file and when I use the below code to read the values, its throwing a classNotFound exception. I would like to know if there is a way, I can handle this exception, i.e once it comes to the exception block, can I replace the classname in the object that is being read to the new class name and make it work ?



Any help on this is much appreciated. Thanks in Advance
 
Tony Docherty
Bartender
Posts: 3210
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is an easy solution to this. You could try one of the following:

1. Resurrect your original class so you can re-create the objects and add a method to the class (eg called toXXX) to build an object of the new class name type from the data in the object. Note if the class doesn't currently have a serialVersionUID you must generate the number using the serialver tool and set it to that number before you change the class file.
2. Get a hex editor and edit serialized file to specify the new class name or provide your own FilterInputStream that does this on the fly.
3. Extend ObjectInputStream and override the readClassDescriptor method to return the appropriate object stream class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!