Finding serialVersionUID of serialized object

2019-02-03 07:20发布

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.

9条回答
趁早两清
2楼-- · 2019-02-03 07:57

The easiest way to get it (especially on hard to find cases) is to define any number for serialVersionUID and monitor the stacktrace when the deserialization fails, it will print out the original one.

查看更多
Emotional °昔
3楼-- · 2019-02-03 07:59

There is a specified grammar for the serialization of objects:

See chapter 6.4 in http://java.sun.com/javase/6/docs/platform/serialization/spec/serialTOC.html

Using this, you should be able to determine the SerialVersionUID of your serialized object.

查看更多
神经病院院长
4楼-- · 2019-02-03 08:04

That's exactly what you should do - specify your own static final long serialVersionUID.

There's a section about it in the docs for Serializable.

Unless you've specified a serialVersionUID I don't believe there's an easy way to get it other than deciphering the stream as @WMR suggests.

查看更多
登录 后发表回答