Search code examples
javaserializationserialversionuid

Java - Modifying serialVersionUID of binary serialized object


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.

Is there a way to either disable the check or to modify the serialVersionUID in the binary file?

CLARIFICATION

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)?


Solution

  • As a hack, you can generate the serialVer your jvm is probably using using the serialver tool:

    serialver -classpath whatever com.foo.bar.MyClass

    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.