8

I have an object as follows:

    public class Records implements java.io.Serializable{
        private int cId;
        private int pId;
        private int vlaue;
        private int tag;

        public Records(int c, int p, int v, int t){
                this.cId=c;
                this.pId=p;
                this.value=v;
                this.tag=t;
        }
}

I've collected lots of data, constructed objects as in the above class and seralized them to disk.

One dump thing I've forgotten to include in the class file is methods to access the values for each object. For example, to access the cId value for a particular object.

I modified the class definition to add such methods but then I could not deseralize the objects back to Records class and get this runtime error:

java.io.InvalidClassException: Records; local class incompatible: stream classdesc serialVersionUID = -1232612718276774474, local class serialVersionUID = -8244963718951538904
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:579)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1600)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1513)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1749)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)
    at DeSerialise.main(DeSerialise.java:21)

I think I need to tell java that they are the same definitions and modify the serialVersionUID but not quite sure how? any idea would be welcomed!

Android Killer
  • 18,174
  • 13
  • 67
  • 90
DotNet
  • 697
  • 2
  • 7
  • 23
  • 1
    You will find good explanation of your problem on this question: http://stackoverflow.com/q/285793/1916110 – Tom Jan 24 '13 at 15:04

4 Answers4

9

Try adding the following to your class:

private static final long serialVersionUID = -1232612718276774474L;

That'll bring your class's serialVersionUID in line with the compiler-generated value used when the instance was serialized.

The following quote from the documentation is worth reading (emphasis mine):

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID 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 declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private 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 explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

You can declare the serial version id in your class as follows:

private static final long serialVersionUID = -1232612718276774474L;
Jerry B
  • 135
  • 1
  • 9
  • 1
    adding a random serialVersionUID won't help. – JB Nizet Jan 24 '13 at 15:04
  • @JBNizet is right. A random serialVersionUID doesn't work and would not even match the one java compiler has picked as in the above answer. – DotNet Jan 24 '13 at 15:22
  • 1
    If you don't include a serial version UID, Java generates one for you. When deserializing, it checks that the serial version UID of the class matches with the one stored in the serialized bytes. So if you want to deserialize a byte array, your class must have the same UID as the one that Java generated and wrote in the byte array when the object was serialized. And of course, the fields of the class must be compatible. – JB Nizet Jan 24 '13 at 15:26
  • adding a random id won't help de-serialze existing serialized objects that have a different id, but it will work for any objects serialized after you add that random id – Jerry B Jan 25 '13 at 00:35
  • @JrryB Of course but this question is about the former case, and the error message provides the correct value required. So this is not an answer. – user207421 Jan 25 '13 at 06:02
  • @JBNizet, if you didn't use an IDE like Eclipse that generates your # for you, how would you go about generating a proper `serialVersionUID`? – amphibient Jan 25 '13 at 23:06
  • 1
    @foampile You can leave it out and get it from the first exception, as here, or you can use the serialver tool. – user207421 Jan 26 '13 at 04:24
  • so are you saying don't bother using the Eclipse generated #? – amphibient Jan 26 '13 at 16:51
  • @foamplie Whenever I see a question that starts 'so are you saying', the answer is invariably 'no'. You asked how to get the serialVersionUID *without* an IDE like Eclipse. I answered that question. How you can possibly turn that into a remark about Eclipse is a complete mystery. – user207421 Jan 28 '13 at 00:12
0

Yes its very important that you specify serialversionId for class. Otherwise it will be difficult to identify serialized class.

You can also create new serialized object instead of using old serialized object if you are doing so.

Eclipse IDE will automatically generate serialversionId try to use that.

Brijesh
  • 191
  • 1
  • 5
  • 11
0

The problem is that you serialized data without having a serialVersionUID defined in your class. Adding setters and getters is ok and does not impact serialization, but the compiler might generate another UID when you recompile the class which will lead to an exception when you try to deserialize your data. If need to actually deserialize the data you serialized using the first version of your class you will need to know what UID was generated by the compiler the first time: this can be seen in the error message, so you need to set serialVersionUID to -1232612718276774474L.

Bogdan
  • 934
  • 7
  • 13