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