Friday, March 19, 2010

Brief About InvalidClassException in Java


Hi Folks,

How are you all? Today i have a got an error below which stop me to work for a couple of hours

Unable to de-serialize job. Blah.... local class incompatible: stream classdesc serialVersionUID = -9148880418277643823, local class serialVersionUID = 2323422343
java.io.InvalidClassException: ClassName local class incompatible: stream classdesc serialVersionUID = -9148880418277643823, local class serialVersionUID = 2323422343

I thought most of the folks facing this error some time and start searching google for solution. Then i also thought i share a light of my work around here.

Generally InvalidClassException comes when your class implements Serializable interface and you didn't mention serialVersionUID field in your class. If your class doen't contain this field when ever your class compiles, compiler generates a unique id like roll no which causes conflicts with old one results exception.

To avoid this problem, solution is very simple. You need to enter below field in your class when it extends Serializable interface.

public static final long serialVersionUID = 34354354435l;

This small filed eliminates problem causing InvalidClassException

When you are programming your java code in Eclipse then you get a warning like "The serializable class FlattenAllWorkingSetsJob does not declare a static final serialVersionUID field
of type long" this. If you try to eliminate that warning that automatically resolves problem of causing InvalidClassException