How can I find out the serialVersionUID of a serialized Java object?

前端 未结 3 1365
春和景丽
春和景丽 2021-02-13 16:28

I have several serializable classes that were compiled without specifying a serialVersionUID. I now need to add some data members to these classes but wish to preserve some alre

相关标签:
3条回答
  • 2021-02-13 17:00

    If you are using Eclipse, it has a "quick fix" to generate the calculated serialVersionUID. This of course only works if you have the (exact) source for the classes at the time the objects have been serialized.

    If you have at least the class files for them, use the "serialver" command line tool (comes with JDK).

    0 讨论(0)
  • 2021-02-13 17:01

    If you haven't modified the code yet you could probably run the serialver tool (part of the JDK) and see what it generates. I believe that will get you what the calculated seriVersionUID is.

    Here is a brief article on serialver.

    0 讨论(0)
  • 2021-02-13 17:03

    What I usually do is, I compile the code, try to run it, and the raised exception mentions the offending SVUID, so I can cut and paste it in my own code. Crude, but it works.

    0 讨论(0)
提交回复
热议问题