aspose file tools*
The moose likes Java in General and the fly likes ClassLoader problem :ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

ClassLoader problem :ClassCastException

Peter Kovgan
Ranch Hand

Joined: Sep 18, 2003
Posts: 84
Hi all!
Please help me resolve the problem:
When I make JNDI lookup to get reference to some server object :
SomeClass mbs = (SomeClass) initCtx.lookup("some");
I receive ClassCastException, I know my server use different class loader to load this object.
Somebody knows how to make casting when classes loaded by different class loaders?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

So you are finding that the following code reports the same Class but different ClassLoaders?

If this is the problem you are having, there is a very nasty fix where you get a PipedStream with ObjectStreams at either end. You get your instance as an Object, pass it through the piped Stream and then do the cast at the other end. I don't have sample code available, but I can go look if you want.
Dave.
Peter Kovgan
Ranch Hand

Joined: Sep 18, 2003
Posts: 84
Please!!! Give me example!!!
Please!!!
Peter Kovgan
Ranch Hand

Joined: Sep 18, 2003
Posts: 84
I tried to do somthing, but have a new problem: lookupED object does not Serializable, then ObjectOutputStream oos = new ObjectOutputStream(obj); does not work, can you send me another example?

If we passed through streams, the reference to remote object still avaiable?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

OK, I found it.
I really do have to say 'Use this at your own risk'. We should probably start to look at your setuo and find out why you are having ClassLoader problems. I consider the following piece of code a horrible hack that is designed to hide problems, not fix them.

If we passed through streams, the reference to remote object still avaiable?
Yes and No. The new object loses the reference, but you still have access to the old object. You'll need to decide what to do about that if you need call-backs. You might need to create a proxy that wraps the problem class and passes method calls via reflection...
I hope this gets you started though.
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Please don't post the same question in multiple forums, it creates duplicate conversations and annoys the people trying to help you.
Anyone who wants to contribute should see this thread.
Dave
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

I hereby close this topic.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ClassLoader problem :ClassCastException