Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.InvalidClassException: local class incompatible:

I created client and server and then added a class in client side for serializing purposes, then simply just went to the folder of the client in my hard drive and copy paste it to the server correponding location, both classname.class and classname.java respectively.

It worked well in my own laptop but when I wanted to continue my work on other system , when I opened the projects folders and after client tries to connect to the server, the following error appears:

Exception in thread "main" java.io.InvalidClassException: projectname.clasname; local class incompatible: stream classdesc serialVersionUID = -6009442170907349114, local class serialVersionUID = 6529685098267757690     at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:562)     at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1582)     at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1495)     at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)     at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350) 

What is going on? Is it because I ran the program with an older version of the IDE?

EDIT

import java.io.Serializable; import java.net.URL;  public class KeyAdr implements Serializable {   private static final long serialVersionUID = 6529685098267757690L;    public URL adr;   public String key; } 
like image 969
lonesome Avatar asked Apr 30 '12 05:04

lonesome


People also ask

Can 2 classes have same serialVersionUID?

Technically you can't prevent two classes from having the same serial version UID, much like you can't prevent two objects from having the same system hash code.

Why do we need serialVersionUID in Java?

The SerialVersionUID must be declared as a private static final long variable in Java. This number is calculated by the compiler based on the state of the class and the class attributes. This is the number that will help the JVM to identify the state of an object when it reads the state of the object from a file.

What is serialVersionUID 1l in Java?

The serialization at 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.

What is a serialVersionUID in Java?

SerialVersionUID is a unique identifier for each class, JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization. Specifying one gives more control, though JVM does generate one if you don't specify.


Video Answer


2 Answers

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 serialVersionUIDs 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 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.

You should define a serialVersionUID in the class definition, e.g.:

class MyClass implements Serializable {     private static final long serialVersionUID = 6529685098267757690L;     ... 
like image 51
trutheality Avatar answered Sep 21 '22 09:09

trutheality


One thing that could have happened:

  • 1: you create your serialized data with a given library A (version X)
  • 2: you then try to read this data with the same library A (but version Y)

Hence, at compile time for the version X, the JVM will generate a first Serial ID (for version X) and it will do the same with the other version Y (another Serial ID).

When your program tries to de-serialize the data, it can't because the two classes do not have the same Serial ID and your program have no guarantee that the two Serialized objects correspond to the same class format.

Assuming you changed your constructor in the mean time and this should make sense to you.

like image 21
belka Avatar answered Sep 22 '22 09:09

belka