I had saved one java object in the Database and then after few days I changed my jre version. Now when i tried to read that same object I am getting following exception:
Exception in thread "main" java.io.InvalidClassException:
SerializeMe; local class incompatible: stream classdesc
serialVersionUID = -6377573678240024862, local class serialVersionUID = -8204757486033751616
How can I get rid of this,how can I get the saved object?
please help me.
Geek, now you must be wondering why do we use SerialVersionUID? It is because SerialVersionUID is used to ensure that during deserialization the same class (that was used during serialize process) is loaded. Consider ran the illustration given below to get a fairer understanding of Serialization & Deserialization.
SerialVersionUID is a unique identifier for each class, JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization. Specifying one gives more control, though JVM does generate one if you don't specify.
The SerialVersionUID must be declared as a private static final long variable in Java. This number is calculated by the compiler based on the state of the class and the class attributes. This is the number that will help the JVM to identify the state of an object when it reads the state of the object from a file.
The serialization runtime associates with each serializable class a version number, called a serialVersionUID , which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.
If you can affect source code of this class and JRE was only thing that changed, most likely you can still deserialize object that was serialized by older JVM. Just define following field in class to be deserialized:
private static final long serialVersionUID = -6377573678240024862L;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With