EN
Java - Exception in thread "main" java.io.InvalidClassException: logic.User; class invalid for deserialization
1
answers
4
points
I would like to write program that deserialize object using ObjectInputStream class but it throws exception, even if ObjectOutputStream class was used to serialize object.
Exception details:
Exception in thread "main" java.io.InvalidClassException: logic.User; class invalid for deserialization
at java.base/java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:159)
at java.base/java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:875)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2170)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1679)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:493)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:451)
at logic.Program.main(Program.java:20)
1 answer
7
points
Do you have defined static final long serialVersionUID filed at the beginning in your User class? If not, try to use some random value there.
That filed is used to identify the object, preventing against type mistakes when we serialize and deserialize object to binary.
Example:
class User {
private static final long serialVersionUID = 1406927626238015433L;
// ...
}
Practical example is avaialble here.
0 comments
Add comment