Java - 修改二进制序列化对象的serialVersionUID [英] Java - Modifying serialVersionUID of binary serialized object
问题描述
几个月前,我将java.io.Serializable对象序列化为一个文件。现在我需要读取内容,但从那时起serialVersionUID已经改变,现在我遇到了类不兼容的错误。我知道没有数据成员发生过变化,因此唯一的障碍是serialVersionUID检查。
A few months back I serialized a java.io.Serializable object into a file. Now I need to read the contents, but since then the serialVersionUID has changed, and now I'm getting a "class incompatible" error. I know for a fact that none of the data members have changed, so the only barrier is the serialVersionUID check.
有没有办法可以禁用检查或修改二进制文件中的serialVersionUID?
Is there a way to either disable the check or to modify the serialVersionUID in the binary file?
CLARIFICATION
CLARIFICATION
这个问题假设我无法编辑源代码。有没有办法可以破解.class文件或者破解序列化的目标文件(使用十六进制编辑器并在某个偏移处更改一个值)?
This question is assuming that I can't edit the source. Is there a way I can hack the .class file or perhaps hack the serialized object file (use a hex editor and change a value at some certain offset)?
推荐答案
作为一个hack,你可以使用serialver工具生成你的jvm可能正在使用的serialVer:
As a hack, you can generate the serialVer your jvm is probably using using the serialver tool:
serialver -classpath com.foo。 bar.MyClass
serialver -classpath whatever com.foo.bar.MyClass
如果您在类中手动设置serialVerUID它应该匹配并且您应该能够加载,假设您没有更改类这样一种无效的方式。
If you then manually set the serialVerUID in your class it ought to match and you ought to be able to load, assuming you haven't changed the class in such a way as to invalidate.
这篇关于Java - 修改二进制序列化对象的serialVersionUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!