how resolve java.io.InvalidClassException: local class incompatible: stream classdesc serialVersionUID
Define a member in the class concerned:
private static final long serialVersionUID = 1113799434508676095L;
This solves the problem described in the message, by forcing the serialVersionUID
of the local class to match what was put in the stream when you serialized the old version of the class, which would have been computed automatically over various class attributes if no serialVersionUUD
field was present at that time.
It is possible that you altered the class in an incompatible way, which will then produce a different exception. See the Object Serialization Specification chapter on Versioning for details. However merely adding or removing a field doesn't have that effect.
You can implement private void readObject(ObjectInputStream in)
in your object class. It kinda "overrides" the default behavior, even though it is declared as "private", so technically it should not. Basically, when java serializer needs to read an object from a stream, of class that has this method implemented, it will call that instead of doing its default thing.
So, you can implement a logic into it, that will read all the existing fields from the stream, and assign default values to those that are missing.
Edit: As @EJP points out (thanks, @EJP!), this does not quite work. You also need to defineprivate static long serialVersionUID
in your class, and set it to the "old" value you see in the exception.
Also, consider replacing Serializable with Externalizable for the future, it gives you some more flexibility, and transparency. Extending Externalizable
tells java that you intend to handle serialization yourself, and it will then not attempt to do its default thing that throws the exception.
In that case, implement readExternal(ObjectInputStream in)
to read members from the stream one-by-one, and initialize those that are missing to some kind of default.