Add to Google Reader or Homepage

java.io.invalidclassexception: local class incompatible

Invalid Class Exception:Local class incompatible

    This exception is thrown when the serial version of the class is found to be different from  that of the class descriptor read from the stream.

Whenever object serialization is performed the objects are saved in a particular file format.This file format contains a class descriptor for each class of the object that is saved.

The class descriptor usually contains the 
  • class name
  • serial version unique ID
  • set of flags
  • description of the data fields
If the value of the serial version unique Id  is not explicitly specified then the jvm will automatically assigns the value for this variable.

We can also be able to assign value for this variable like 1L or 2L.

One thing should be importantly noted that the serial version unique Id should  be same during object serialization and deserialization.

 During serialization this serial version unique Id value would be recorded in the class descriptor.

While deserialization current serial version unique Id value would be compared with the one in the class descriptor.If there is any mismatch between the values this exception would be thrown.

I have also included the example program of object serialization and indeed i have changed the serial version unique Id value during deserialization.

Program:

Invalid.java

import java.io.*;
import java.util.*;
public class Invalid implements Serializable
{
public static final long serialVersionUID =2L;

String empname;
int empno;

public Invalid(String name,int no)
{
empname=name;
empno=no;
}
public String getname()
{
return empname;
}
public int getno()
{
return empno;
}
}

Writeobject:


import java.io.*;
import java.util.*;
public class Writeobject
{

public static void main(String args[])
{

Invalid iv=new Invalid("shaun",10);
 try
   {

            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat"));
            System.out.println(iv.getname()+iv.getno());
            out.writeObject(iv);
            out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Readobject:


import java.io.*;
import java.util.*;
public class Readobject
{

public static void main(String args[])
{
Invalid inv=null;
 try
   {

         ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"));
           inv = (Invalid) in.readObject();
           in.close();
           System.out.println(inv.getname()+inv.getno());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Output:

C:\>cd manet

C:\manet>javac Invalid.java

C:\manet>javac Writeobject.java

C:\manet>java Writeobject

shaun10

C:\manet>javac Readobject.java

C:\manet>java Readobject

shaun10

C:\manet>javac Invalid.java

C:\manet>javac Readobject.java

C:\manet>java Readobject

java.io.InvalidClassException: Invalid; local class incompatible: stream classde

sc serialVersionUID = 1, local class serialVersionUID = 2

        at java.io.ObjectStreamClass.initNonProxy(Unknown Source)

        at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)

        at java.io.ObjectInputStream.readClassDesc(Unknown Source)

        at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)

        at java.io.ObjectInputStream.readObject0(Unknown Source)

        at java.io.ObjectInputStream.readObject(Unknown Source)

        at Readobject.main(Readobject.java:13)


Note that  when executing the above programs for the second time i have purposefully changed the Serial Version Unique Id  to 1L while reading the objects(de-serializing).Because of this invalid class Exception is thrown.

1 comments:

iteducationcentre said...

thanks for the post.
Java training in Pune

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top