Ravi Jain
Ravi Jain

Reputation: 1509

Getting java.io.InvalidClassException

I am getting below exception at runtime whenever i am trying to run it in my project.

EXCEPTION MESSAGE :Message : java.io.InvalidClassException: com.mypackage.abc.MyClass; local class incompatible: stream classdesc serialVersionUID = -297991290629036654, local class serialVersionUID = -6182496564283649260

Can someone help me to get rid from this exception. What is the reason behind this and how can i resolve this?

Upvotes: 0

Views: 579

Answers (3)

Adel Boutros
Adel Boutros

Reputation: 10285

From the java docs:

Thrown when the Serialization runtime detects one of the following problems with a Class.

The serial version of the class does not match that of the class descriptor read from the stream
The class contains unknown datatypes
The class does not have an accessible no-arg constructor

Upvotes: 0

Fabian Barney
Fabian Barney

Reputation: 14549

You're trying to read a serialized object which corresponding class definition changed.

Upvotes: 0

Jesper
Jesper

Reputation: 206886

The API documentation of InvalidClassException says:

Thrown when the Serialization runtime detects one of the following problems with a Class.

  • The serial version of the class does not match that of the class descriptor read from the stream
  • The class contains unknown datatypes
  • The class does not have an accessible no-arg constructor

Looking at the error message, the first of those three is what's happening in your case.

It looks like you are trying to load a serialized object, but in the meantime you have made changes to your class com.mypackage.abc.MyClass so that the serialized object you're now trying to load is incompatible with the new version of the class.

One problem with Java's standard serialization mechanism is that it ties the serialized objects very closely to your source code: if you change your source code (specifically, your com.mypackage.abc.MyClass class), then old serialized objects cannot be read anymore.

One way to solve this is to revert back to the old version of com.mypackage.abc.MyClass, then write a small tool to load the serialized objects, and save them back in a different file, in a standard, extensible, well-documented format (for example XML) instead of using Java serialization.

Upvotes: 3

Related Questions