Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java RMI

 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please excuse me if i am asking the obvious or something which has been previously discussed. I searched a little for this.... but i didnot know what exactly to search. I will keep searching on google to find out if I get an answer.

I have been exposed to RMI for the first time after learning Languages. One thing felt a little striking.



Consider the implementation




Now the client








I realized that the 2nd optioin is the right option. However, if I use the first, it gives me an error at runtime(Invalid cast). Now as far as I know, since the abstract interface method sayHello is defined only in the RMIImplementation class. So effectively any class which is returned by Naming.lookup will be a subclass of RMIImplementation and thus of RMIInterface.
If this is the case, why is there a problem in the casting?

What exacatly is the type of the returned object if it is not RMIImplementation but still is a type of RMIInterface
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please assume minor mistakes are taken care of before compiling. and the boilerplate code is correct
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be a class created automatically by the JVM for this purpose; it won't be related to "RMIImplementation" in any way. In fact, the client program doesn't need the RMIImplementation class file to be on its class path.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, I'll move this to our Distributed Java forum - the place for RMI on JavaRanch.

Gaurav Raje wrote:I realized that the 2nd optioin is the right option. However, if I use the first, it gives me an error at runtime(Invalid cast). Now as far as I know, since the abstract interface method sayHello is defined only in the RMIImplementation class. So effectively any class which is returned by Naming.lookup will be a subclass of RMIImplementation and thus of RMIInterface.
If this is the case, why is there a problem in the casting?

What exacatly is the type of the returned object if it is not RMIImplementation but still is a type of RMIInterface

It's RMIImplementation_Stub. The rmic compiler creates two helper classes for your RMIImplementation class: RMIImplementation_Skel and RMIImplementation_Stub. These two wrap around your RMIImplementation class to handle the communication. The Stub class also implements the interface, and that's why casting to the interface works and casting to the original class doesn't.
You can verify as such:
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply....
I did check the class of the returned object. But i always thought it was still a subclass of the implementation. Hence was confused...

Thanks for clearing my doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic