ClassLoader and deserialization
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Bogdan_Matasaru
Posted On:   Friday, November 18, 2005 12:19 PM

I use an URLClassLoader to load a class from a jar, then I instantiate an object and serialize it. What can I do to deserialize it? The class is not in the CLASSPATH and I cannot specify to the ObjectOutputStream which ClassLoader to use. I cannot add the jar file to the CLASSPATH so it has to be using an URLClassLoader . ... URLClassLoader l1 = URLClassLoader.newInstance(new URL[] {new URL("file:test.jar")}); Object a = l1.loadClass("MyClass").newInstance(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeOject(a); oos.close();    More>>

I use an URLClassLoader to load a class from a jar, then I instantiate an object and serialize it.

What can I do to deserialize it? The class is not in the CLASSPATH and I cannot specify to the ObjectOutputStream which ClassLoader to use. I cannot add the jar file to the CLASSPATH so it has to be using an URLClassLoader .


			
...
URLClassLoader l1 = URLClassLoader.newInstance(new URL[] {new URL("file:test.jar")});
Object a = l1.loadClass("MyClass").newInstance();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeOject(a);
oos.close();
byte[] data = bos.toByteArray();
...
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
a = ois.readObject();
...


A ClassNotFoundException is thrown when trying to read the object from ois. Of course, it's what everybody expects but how can I solve the problem?


Thanks,

Bogdan

   <<Less

Re: ClassLoader and deserialization

Posted By:   Bogdan_Matasaru  
Posted On:   Friday, February 10, 2006 10:19 PM

It works if I derive a class from ObjectOutputStream and in the method resolveClass I can load the class with my class loader.

Re: ClassLoader and deserialization

Posted By:   Marcos_Luque  
Posted On:   Tuesday, January 3, 2006 08:02 PM

You could import the JAR file in the header of the program, and then you can use the classes inside of the file.

Re: ClassLoader and deserialization

Posted By:   Almagest_FUTT  
Posted On:   Monday, November 21, 2005 07:01 AM

About | Sitemap | Contact