I have to work with a large number of compiled Java classes which didn’t explicitly specify a serialVersionUID. Because their UIDs were arbitrarily generated by the compiler, many of the classes which need to be serialized and deserialized end up causing exceptions, even though the actual class definitions match up. (This is all expected behavior, of course.)
It is impractical for me to go back and fix all of this 3rd-party code.
Therefore, my question is: Is there any way to make the Java runtime ignore differences in serialVersionUIDs, and only fail to deserialize when there are actual differences in structure?
If you have access to the code base, you could use the SerialVer task for Ant to insert and to modify the
serialVersionUIDin the source code of a serializable class and fix the problem once for all.If you can’t, or if this is not an option (e.g. if you have already serialized some objects that you need to deserialize), one solution would be to extend
ObjectInputStream. Augment its behavior to compare theserialVersionUIDof the stream descriptor with theserialVersionUIDof the class in the local JVM that this descriptor represents and to use the local class descriptor in case of mismatch. Then, just use this custom class for the deserialization. Something like this (credits to this message):