Is there a way to determine the generated serialVersionUID
of a serialized Java object?
The problem is that I serialized an object without explicitely specifying the serialVersionUID
. Now the deserialization process complains about class incompatibilities. However I didn't change the class in a way which would make it incompatible. So I assume that it is enough to specify the serialVersionUID
in the class as it is stored in the object data. In order to do this I need to read the serialVersionUID
from the serialized data.
There is metadata associated with the serialized bits (a header if you like). You can read the value from the metadata if you know at which position it is (the
SerialVersionUID
is written there along with other info such as the class name).I think this article might help you: The Java serialization algorithm revealed.
Note that the bits are written "in clear" (unless you encrypted the stream explicitly) so a HEX editor might be all you need to see what is the
SerialVersionUID
.This works for me:
Hope it helps you too.
There's an easy way to find out serialversionUID of a class-
Suppose you have class in which you have forgotten to mention serialversionUID-
Just do this-
This prints-
serialver is a utility that comes along with JDK
for Android developers,
you can also enable serialization inspection from Settings -> Editor -> Inspections -> enable "Serializable class without 'serialVersionUID' check" first, then ALT+ ENTER, the studio will create serialVersionUID for you.
You can do this by extending ObjectInputStream:
I believe it would be possible to read all the serialized data by replacing the descriptor returned by the method, but I haven't tried it.
there is much simpler way to get the serialVersionUID of serialized object - just deserialize it with apache commons serialization utils to same class with different serial version uid and read the exception message which will be more or less: