Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of InvalidClassException SerialVersionUID?

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.

like image 426
milind_db Avatar asked Nov 28 '12 06:11

milind_db


People also ask

Is SerialVersionUID necessary?

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.

What is SerialVersionUID what would happen if you don t define this?

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.

How to declare SerialVersionUID in Java?

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.

What is serialization and deserialization what is significance of SerialVersionUID?

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.


1 Answers

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;
like image 121
Mikko Maunu Avatar answered Oct 13 '22 19:10

Mikko Maunu