Java deserialization java.lang.Integer - exception
Deserializing a HashMap<String, Integer>
threw the following exception:
java.io.InvalidClassException: java.lang.Integer; local class incompatible: stream classdesc serialVersionUID = 1360826667802527544, local class serialVersionUID = 1360826667806852920
Serialized and deserialized on the same machine with the same JRE. JDK 1.6.0_12
From a look at the JDK source, 1360826667806852920 is correct serialVersionUID
for Integer
. I could not find any classes in the JDK using serialVersionUID
1360826667802527544.
Interestingly, a Google search for 1360826667802527544 attracted a few more people with this problem, especially this thread on the Sun forums. The problem was that the person was storing bytes in a String and the serialized data was getting corrupted. Since you are getting the same thing serialVersionUID
, it seems very likely that you are facing a similar problem.
Never store bytes in String
. Use a byte array or a class designed to store bytes, not characters.
a source to share
check source code for Integer, here is what I have for Integer in multiple java versions:
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1360826667806852920L;
So, I would say that the problem comes from your class that you changed between serialization and deserialization, and which does not have a specific serialVersionUID ...
Maybe you should look in this , the same description of the problem and looks like wrong serialization / deserialization code ...
a source to share
I faced the same problem, and because when we try to store an Integer object to a String, the character encoding gets messed up and the serialVersionUID reading is wrong on deserialization. This is the main reason for this error. To avoid this error, use Base64 encoding before storing it in String. see this answer and the problem solved for me.
/** Read the object from Base64 string. */
private static Object fromString( String str ) throws IOException, ClassNotFoundException {
byte [] data = Base64.getDecoder().decode(str);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
Object o = ois.readObject();
ois.close();
return o;
}
/** Write the object to a Base64 string. */
private static String toString( Serializable o ) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( o );
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
a source to share