Add to Google Reader or Homepage

ClassNotFoundException thrown when deserializing an object

classNotFoundException thrown because of deserialization

 

I guess you may all know about ClassNotFoundException which is thrown,when the class that we try to load(using forName() and loadClass()) is not available in the classpath. Note that this exception is different from the NoClassDefFounderror which is thrown when the SystemClassLoader  tries to load in the class that is not available. 

In both cases the cases one thing remains same, i.e, the class definition is not available in the classpath.

Here,this ClassNotFoundException is thrown  when deserializing an object. Lets see how?

Have you ever wondered what really happens when we serialize an object?You may have read that the state of the object is stored in the stream.What do they really mean by storing the state of the object?

Actually what happens is the values of non-static  and non-transient fields of the class, corresponding to the object that we serialize is stored in the stream in binary format.

When you try to deserialize an object we are not actually retrieving the objects from the stream.We are constructing a new object with the state information provided in the stream    ( i.e, field values stored in the stream).
  
 "we can  construct an object only if the class is available"

So at the time of deserialization we need the actual class to construct that object. Thus it is obvious that if the class is not available in the classpath then this exception would be thrown.

I have explained this with an example as follows.

Program:


 People


import java.io.*;
import java.util.*;
class People implements Serializable
{
String Name;
}
 

Employee


import java.io.*;
import java.util.*;
class Employee extends People
{
public static void main(String[] args) throws Exception{
People p=new People();
p.Name="ganesh";
ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("out.ser"));
os.writeObject(p);
}
}

 
 These two programs are  used to create an object of people and serialize it into the stream.

The following program tries to deserialize the people object from the stream and see what happens when the people class is not available in the classpath.


PeopleRead


import java.io.*;
import java.util.*;
class PeopleRead
{
public static void main(String args[])throws Exception
{
FileInputStream fis = new FileInputStream("out.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o =ois.readObject();// throws ClassNotFoundException
ois.close();
fis.close();
}
}


Exception thrown:

 

Exception in thread "main" java.lang.ClassNotFoundException: People
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at java.io.ObjectInputStream.resolveClass(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 PeopleRead.main(PeopleRead.java:9)
 


Actually i have changed the classpath and compiled & executed it.As you see the line Object o =ois.readObject(); in the above program,it tries to read the state information from the stream and tries to reconstruct the object.Since the class definition is not available i got this exception. You may ask why the hell i am going to change the classpath. 

By the way you are not going to change the classpath, and these kind of exceptions will be thrown when you are dealing with Remote Method Invocation(RMI) where in,the class definition, may reside on the other virtual machine.

3 comments:

Unknown said...

how to fix it?

SANDY said...

Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.

AWS Training in Rajaji Nagar | Amazon Web Services Training in Rajaji Nagar


Amazon Web Services Training in Pune | Best AWS Training in Pune

AWS Online Training | Online AWS Certification Course - Gangboard

Unknown said...

Im obliged for the blog article.Thanks Again. Awesome.
java training
java online training

Post a Comment

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