简体   繁体   中英

java.io.InvalidClassException while deserializing object after some modification in a java class

Why failed to deserialized however I have serialized this Container class using serialVersionUID = 75264711556228L;

public class Container implements Serializable {
    private static final long serialVersionUID = 75264711556228L;
    public boolean isFromLocalCollect=false;
    public boolean isFromLocalCollect=false;
    public void setFromLocalCollect(boolean isFromLocalCollect) {
        this.isFromLocalCollect=isFromLocalCollect;
    }
    public boolean getFromLocalCollect() {
        return this.isFromLocalCollect;
    }
}

now i have added one more setter and getter method in Container class after serialized with name container.ser like:

public class Container implements Serializable {
    private static final long serialVersionUID = 75264711556228L;
    public boolean isFromLocalCollect=false;
    public boolean isFromLocalCollect=false;
    public void setFromLocalCollect(boolean isFromLocalCollect) {
        this.isFromLocalCollect=isFromLocalCollect;
    }
    public boolean getFromLocalCollect() {
        return this.isFromLocalCollect;
    }
    public boolean isFromLocalCollect2=false;
    public void setFromLocalCollect2(boolean isFromLocalCollect2) {
        this.isFromLocalCollect2=isFromLocalCollect2;
    }
    public boolean getFromLocalCollect2() {
        return this.isFromLocalCollect2;
    }
}

Then I am trying to deserialize the object using project name changed but failed to deserialized and error i got like:

java.io.InvalidClassException: cvb.db.PbDb; local class incompatible: stream classdesc serialVersionUID = -444070985558173412, local class serialVersionUID = 4350771162641935418

When you serialized the object you used a version of the Container.class differents from the Container.class used to deserialize it.

You need to use always the same .class to be sure that the operation of serialization deserialization will work correctly.

Actually what i did that i just used this static final serialVersionUID variable in cvb.db.PbDb class like:

private static final long serialVersionUID = 75264711556227L;

and in Container class were already declared serialVersionUID explicitly. After that i am serializing and deserializing with different application context path so exception has gone away and now it's working fine for me.

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