aspose file tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes RMI ClassCastException?  Look here... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Reply Bookmark "RMI ClassCastException?  Look here..." Watch "RMI ClassCastException?  Look here..." New topic
Author

RMI ClassCastException? Look here...

Dave Teare
Ranch Hand

Joined: Oct 09, 2002
Posts: 80
Hi all,
I have seen a lot of people having trouble with class cast problems within their RMI client. I have had this problem several times, and have finally found out why it is happening.
In the client, the Stub returned from the Naming.lookup() must be cast to the interface of the stub, NOT the implementing class of the Stub.
For example, consider the following connection manager of mine:

In my client, I retreive the Stub as follows: Object tmp = Naming.lookup(rmiName);
Now, if I cast tmp to FBNConnectionManagerImpl, things blow up. If I cast it to FBNConnectionManager, everything is fine.
While this might be obvious to those RMI experts out there, as a newbie I thought having an interface that simply implemented Remote seemed unnecessary, so I just had my connection manager implement it directly, and skipped the interface. BIG MISTAKE!

Hope this helps!
--Dave.
Andrea Gazzarini
Ranch Hand

Joined: Sep 09, 2002
Posts: 125
Hi Dave...RMI means Remote Method Invocation...Casting the return of the lookup to the XXXRemoteImpl means that you have the XXXRemoteImpl.class on your local machine!
Greetings


Regards,<br />Andrea<br /> <br /><a href="http://www.linkedin.com/in/andreagazzarini" target="_blank" rel="nofollow">Andrea Gazzarini</a><br />Skype ID : gazzax72<br />Google Talk ID : a.gazzarini@gmail.com<br /><a href="http://www.andreagazzarini.blogspot.com" target="_blank" rel="nofollow">My Blog</a>
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17225
    
    1

Hey Dave,
Thanks for your post, it will be very helpful to many of us.
Now in your solution you are talking about the object that gets bound to the Registry. How about the Connection Object which isn't in the Registry? Will you solution help those classes too?
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Dave Teare
Ranch Hand

Joined: Oct 09, 2002
Posts: 80
Hi Andrea,
On my test machine the 'remote' implementation IS on my local machine!!
That is why I got confused
--Dave.
Dave Teare
Ranch Hand

Joined: Oct 09, 2002
Posts: 80
That is a good question Mark. Again, I simply got it working, I cannot explain WHY it works! From what little I know, and given the previous post, I think you must have interfaces defined for all objects that are returned, and these interfaces must be available to the client.
My understanding is that objects returned from the stub class need only implement Remote and Serializable, for example:

My code can now happily get a connection to the FBN database by simply calling the getInstance() method on the FBNConnectionManager interface.
Sorry for my scattered thoughts; I am just at the dawn of figuring all this RMI stuff out. Once I have everything ironed out, I will try to post a complete overview of my findings.
--Dave.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17225
    
    1

Serializable
Actually If you implement Remote then extend UnicastRemoteObject, you do not need to implement Serializable.
In the case of a straight Remote object you do no want to implement Serializable as the object remains on the server and only the stub is sent to the client.
The only objects that should implement Serializable are those that you are directly sending to the client and resides on the client.
Mark
Dave Teare
Ranch Hand

Joined: Oct 09, 2002
Posts: 80
Mark,
I just tried what you suggested, and yes, you are right - I do not need the Serializable.
Thanks!
BTW - Are you still having your class cast exception?
--Dave.
Reinhold Gruber
Greenhorn

Joined: Sep 20, 2002
Posts: 12
Hi!
Maybe this clarifys some things. If you type

you can investigate the Source of the stub-class
FBNConnectionManagerImpl_Stub.java. This class
and all other stubs extends java.rmi.server.RemoteStub and implements all remote interfaces from FBNConnectionManagerImpl.
Therefore you can't cast the stub to the implementing class, because they have nothing in common besides the remote interfaces.
Reinhold Gruber
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17225
    
    1

Originally posted by Dave Teare:

BTW - Are you still having your class cast exception?
--Dave.

Of course I am.
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RMI ClassCastException? Look here...
 
Similar Threads
Sun's RMI Tutorial
RMI implementation
Need to extend UnicastRemoteObject class
Problem with Testing my RMI BusinessLogic with more than 200 Threads
B&S Stub errors running in network client mode