If child class not it classpath

A

Aaron Fude

Suppose that on the "server", I have the following code:

ObjectInputStream in = new
ObjectInputStream(coming.from.wherever);
ClassA obj= (ClassA) in.readObject();

and I send via the input stream an instance of ClassB that extends
ClassA, but ClassB is not in the classpath of the "server". Should
this work? I would have thought "yes", but I tried it and the answer
is "no".

I have found a workaround so that's not what I am looking for, but I'm
hoping that, through an explanation, I will learn something new.

I see a little bit of what's wrong here. The "server" may want to call
a function on "obj" that's been overridden in ClassB and it doesn't
have access to that code. But is there a way to supply the code the
the "server" as well?

Thanks!

Aaron
 
A

Arne Vajhøj

Aaron said:
Suppose that on the "server", I have the following code:

ObjectInputStream in = new
ObjectInputStream(coming.from.wherever);
ClassA obj= (ClassA) in.readObject();

and I send via the input stream an instance of ClassB that extends
ClassA, but ClassB is not in the classpath of the "server". Should
this work? I would have thought "yes", but I tried it and the answer
is "no".

You can not deserialize an instance of a class that you do not have
a class definition for.

Because it does not know about the members.

Arne
 
A

Aaron Fude

You can not deserialize an instance of a class that you do not have
a class definition for.

Because it does not know about the members.

Arne

That makes sense. And it's not possible to send the definition of the
class over, too?
 
A

Arne Vajhøj

Aaron said:
That makes sense. And it's not possible to send the definition of the
class over, too?

In theory yes.

It just requires a classloader that can read what is loaded
from the stream.

It will be slightly easier if you can use HTTP to load
the class file, because a standard URLClassLoader can
load over HTTP.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top