简体   繁体   中英

Java Deserialization of java.lang.Integer - Exception

Recieved the following exception when deserializing a HashMap<String, Integer> :

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 looking at the JDK source, 1360826667806852920 is the correct serialVersionUID for Integer . I wasn't able to find any classes in the JDK with the serialVersionUID 1360826667802527544.

Interestingly, searching for 1360826667802527544 on Google turned up a few other people with this problem, notably this thread on Sun's forums. The problem there was that the person was storing bytes in a String, and the serialized data was getting mangled. Since you're getting the same serialVersionUID it seems very likely that you're running into a similar problem.

Never store bytes in a String . Use a byte array or a class designed to hold bytes, not chars.

I faced the same issue and it is because when we are trying to store Integer object to String, the character encoding is getting messed up and while deserialization the serialVersionUID read is wrong. That's the root cause of this error. To avoid this error use Base64 encoding before storing it to String. see this answer and the problem resolved 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()); 
    }

check the source code for Integer, here is what I have for Integer in several verions of java:

/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1360826667806852920L;

So I'd say the problem comes from a class of yours that you changed between serialization and deserialization and that has no specific serialVersionUID...

Maybe you should look at this , same problem description and it looks like wrong serialization / deserialization code....

That shouldn't happen. Note that the IDs differ only in the last few digits; the second one ist the one I see in my JDK sources.

My guess is that the serialized stream got corrupted somehow.

I've run into the same issue on a compiled jasperreport. The packed ear on the server was corrupted due to the filtering of the ant build. As a result, the original jasperreport file with the one on the ear had some differences.

I've modified the ant build to copy only the file (instead of filtering) and not filter the

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM