简体   繁体   English

如何找出序列化 Java object 的 serialVersionUID?

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

I have several serializable classes that were compiled without specifying a serialVersionUID.我有几个在没有指定 serialVersionUID 的情况下编译的可序列化类。 I now need to add some data members to these classes but wish to preserve some already serialized objects.我现在需要向这些类添加一些数据成员,但希望保留一些已经序列化的对象。

Is there anyway to find out the serialVersionUID of these serialized objects so that I can specify the same ID in the source code?有没有办法找出这些序列化对象的serialVersionUID,以便我可以在源代码中指定相同的ID?

If you haven't modified the code yet you could probably run the serialver tool (part of the JDK) and see what it generates.如果您尚未修改代码,您可能可以运行 serialver 工具(JDK 的一部分)并查看它生成的内容。 I believe that will get you what the calculated seriVersionUID is.我相信这会让你知道计算出的seriVersionUID是什么。

Here is a brief article on serialver.这是关于serialver的简短文章。

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.我通常做的是,我编译代码,尝试运行它,并且引发的异常提到了有问题的 SVUID,所以我可以将它剪切并粘贴到我自己的代码中。 Crude, but it works.粗鲁,但它有效。

If you are using Eclipse, it has a "quick fix" to generate the calculated serialVersionUID.如果您使用的是 Eclipse,它有一个“快速修复”来生成计算出的 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).如果您至少有 class 文件,请使用“serialver”命令行工具(JDK 附带)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM