java.io.InvalidClassException: local class incompatible:
If a class does not explicitly define a private static final long serialVersionUID
in the code it will be autogenerated, and there is no guarantee that different machines will generate the same id; it looks like that is exactly what happened.
Also if the classes are different in any way (using different versions of the class) the autogenerated serialVersionUID
s will also be different.
From the Serializable
interface's docs:
If a serializable class does not explicitly declare a
serialVersionUID
, then the serialization runtime will calculate a defaultserialVersionUID
value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declareserialVersionUID
values, since the defaultserialVersionUID
computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpectedInvalidClassExceptions
during deserialization. Therefore, to guarantee a consistentserialVersionUID
value across different java compiler implementations, a serializable class must declare an explicitserialVersionUID
value. It is also strongly advised that explicitserialVersionUID
declarations use theprivate
modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID
fields are not useful as inherited members. Array classes cannot declare an explicitserialVersionUID
, so they always have the default computed value, but the requirement for matchingserialVersionUID
values is waived for array classes.
You should define a serialVersionUID
in the class definition, e.g.:
class MyClass implements Serializable {
private static final long serialVersionUID = 6529685098267757690L;
...
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.
Java serialization - java.io.InvalidClassException local class incompatible
@DanielChapman gives a good explanation of serialVersionUID, but no solution. the solution is this: run the serialver
program on all your old classes. put these serialVersionUID
values in your current versions of the classes. as long as the current classes are serial compatible with the old versions, you should be fine. (note for future code: you should always have a serialVersionUID
on all Serializable
classes)
if the new versions are not serial compatible, then you need to do some magic with a custom readObject
implementation (you would only need a custom writeObject
if you were trying to write new class data which would be compatible with old code). generally speaking adding or removing class fields does not make a class serial incompatible. changing the type of existing fields usually will.
Of course, even if the new class is serial compatible, you may still want a custom readObject
implementation. you may want this if you want to fill in any new fields which are missing from data saved from old versions of the class (e.g. you have a new List field which you want to initialize to an empty list when loading old class data).
Java - InvalidClassException local class incompatible serialVersionUID
The serialVersionUID
needs to be defined with the same value as in the stream: -5756298698047880134
.
You must have deployed two different versions of the JDOM library.
Related Topics
Noclassdeffounderror While Trying to Run My Jar with Java.Exe -Jar...What's Wrong
Try with Resources Introduce Unreachable Bytecode
Javafx: "Toolkit" Not Initialized When Trying to Play an Mp3 File Through Mediaplayer Class
How to Make Cartesian Product with Java 8 Streams
Absolute Minimum Code to Get a Valid Oauth_Signature Populated in Java or Groovy
Difference Between Paint() and Paintcomponent()
How to Dynamically Build a Multi-Dimensional Array in Java
Using Superclass to Initialise a Subclass Object Java
Check If a Value Exists in Arraylist
Java Server with Multiclient Communication
Tablecellrenderer and How to Refresh Cell Background Without Using Jtable.Repaint()
Can a Private Method in Super Class Be Overridden in the Sub-Class
Best Language to Parse Extremely Large Excel 2007 Files
How to Upper Case Every First Letter of Word in a String