ObjectInputStream and unknown object

D

David Levitan

I'm having an issue with ObjectInputStreams. I have two JVM instances
running, communicating via ObjectInputStream/ObjectOutputStreams. Call
them instance A and instance B.
The basic way the program works is that A sends B some data, B does
something to it, and sends back a result that is an object containing
several other objects. Until today, this has worked fine, but all
objects being sent back and forth were either part of the code base for
A and B or a standard java package.
Today a situation came up where B is sending back an object that is a
subclass of one of the classes in A/B's codebase, but is not in the
codebase itself. Based on my debugging, B sends the result (which
contains this object) successfully, but A just hangs trying to read the
object. The new object is in B's classpath but is not in A's classpath
This new object seems to be the reason for the hang, since I haven't
seen this behavior before. I'm wondering if anyone can comment on this
and whether dynamically changing the classpath would solve the issue, or
whether there is a more elegant way of solving the issue.
I'll be happy to provide more details if needed.

Thanks,
David
 
I

Ike

Hi David,

Have you tried wrapping the Object(s) passed in a collections class like an
ArrayList, then merely pass the ArrayList, looking therefore for an
ArrayList on the other side?

Only when you iterate through the ArrayList after receiving it, the
unknown-classed object should then throw an exception of some sort,and you
might be able to try-catch it there, after the OutPutStreams have completed
their work?

HTH -Ike
 
D

David Levitan

Ike said:
Hi David,

Have you tried wrapping the Object(s) passed in a collections class like an
ArrayList, then merely pass the ArrayList, looking therefore for an
ArrayList on the other side?

Only when you iterate through the ArrayList after receiving it, the
unknown-classed object should then throw an exception of some sort,and you
might be able to try-catch it there, after the OutPutStreams have completed
their work?

HTH -Ike


The actual object being transmitted is a wrapper class - the instance
receiving the object never gets out of the readObject method. I do know
adding the previously unknown object to the classpath does make it work
(I confirmed this yesterday), but I need to change the path dynamically
and have no idea how to remove directories dynamically (I know how to
add them).
Thanks,
David
 
C

Chris Uppal

David said:
Today a situation came up where B is sending back an object that is a
subclass of one of the classes in A/B's codebase, but is not in the
codebase itself.

It's a bit tricky to manage this. If the two applications don't have a common
set of .class files, then you'll have to find some way to do one of two things:

a) Only transmit objects which /are/ shared. This may force you to convert
objects from one side or the other into some sort of neutral common form before
sending them. Not too nice...

b) Use classloaders to share the code dynamically. Also painful, but perhaps
not as bad.

Before getting into that, it's worth pointing out that RMI already has features
for doing this kind of thing for you, so if you haven't looked at RMI then it
might be worth doing so.

Anyway, concentrating on (b). There are two issues here.

The first is classloaders, what they do, and how they work. If you don't know
how to use classloaders, then now is the time to find out ;-) Once you have
done, then you will understand how to use custom classloaders to control almost
every aspect of where an application looks for class definitions.

The second issue is how to tell the ObjectInputStream to use your custom
classloader. That's a lot simpler -- it's just a matter of creating a custom
subclass of ObjectInputStream which overrides the definition of resolveClass().
In fact you might not even have to do that, if the class which uses the
ObjectInputStream was itself loaded by your custom classloader.

-- chris
 
E

EJP

Ike said:
Have you tried wrapping the Object(s) passed in a collections class like an
ArrayList, then merely pass the ArrayList, looking therefore for an
ArrayList on the other side?

Only when you iterate through the ArrayList after receiving it, the
unknown-classed object should then throw an exception of some sort,and you
might be able to try-catch it there, after the OutPutStreams have completed
their work?

No. Java still has to deserialize the members of the list, so the
problem will still arise in readObject().
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top