Search code examples
javaserializableserialversionuid

serialVersionUID Exception


I'm currently writing a project where I have a serializable class ClientRepository. The class don't specifies any serialVersionUID. When I try to run my program, I got following Exception:

Exception in thread "main" java.io.InvalidClassException: ClientRepository; local class incompatible: stream classdesc serialVersionUID = -477189107700903771, local class serialVersionUID = -3576242897222506440

So I added a default serialVersionUID (1L), but now I get

Exception in thread "main" java.io.InvalidClassException: ClientRepository; local class incompatible: stream classdesc serialVersionUID = -477189107700903771, local class serialVersionUID = 1

How can I change the "stream classdesc serialVersionUID" to 1L, so it won't conflict?

Thanks!


Solution

  • Don't try to change the data - instead, if you're certain that your class is still compatible with the old version (and if only one version has data out in the wild), change your declared serialVersionUID to -477189107700903771L to match the value in the data. The value is arbitrary - it just has to match what's in the data.

    Of course, if your class has changed in an incompatible way, you'll need to write some custom serialization code.

    Personally I try to avoid native binary serialization like this for precisely these brittleness reasons. You might wish to look at alternatives such as Protocol Buffers.