aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes RMI binding problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "RMI binding problems" Watch "RMI binding problems" New topic
Author

RMI binding problems

Robert Wj Wilson
Greenhorn

Joined: May 26, 2009
Posts: 5
Howdy yall!

I've been checking into the ranch for a while now and am coming to the end of my SCJD, well, by that I mean that I want to submit it in the very near future! However....

The problem I'm having is that RMI isn't quite doing what I want it to. I'm building my project with an ANT script and when running the server command, it is not listing my object as being bound to the registry, I'm checking with registry.list(). Similarly, when running the client and attempting to retrieve the remote reference to the bound object, I get a "sun.rmi.registry.RegistryImpl_Stub cannot be cast to suncertify.remote.DataAccessManagerRemote" exception - which I could understand if the server is not binding the object. I've read all the posts I can get my hands on and have read the java RMI tutorial a number of times, but still no luck. I'm building the stubs with the rmic task (I originally thought this was my problem) and they are definitely present in the jar file.

The other interesting point to note is that when I start the server and client from Eclipse (on the same machine) and connect over RMI, they work beautifully, which makes me think Eclipse is handling some classpath config under the bonnet, but I just can't seem to get the RMI to work from outside Eclipse...As far as I'm aware, we don't have to specify the rmi codebase parameter when starting the server....

I'm using 1.6 and I appreciate in advance any help that anyone can give me.

Thanks
Rob
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2531
    
    8

Hello there, it's definitely Eclipse. Try compiling and jar-ing your files with the command prompt. Then run the rmi server. If it starts properly, then you are fine. Else post your errors or the stack trace.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Robert Wj Wilson
Greenhorn

Joined: May 26, 2009
Posts: 5
K. Tsang - You are a genius! I built it from the command line following the instructions in Andrews book and viola! It seems to be working ok with RMI now. Thank you very much for your help!

Rob
Robert Wj Wilson
Greenhorn

Joined: May 26, 2009
Posts: 5
K.Tsang, sorry looks like I spoke too soon about it working. I built the jar on the command line and then run it locally from the command line on the machine i built it on and the RMI portion worked perfectly. As soon as I move the jar to another machine and start the client or server and try to connect them together, I get the same problem - if the server is started then it doesn't show that the object has been bound to the registry, and if the client is started I get a class cast exception sayiong that the stub cannot be cast to my interface.

I've run the server on my local machine and checked the registry with the list() method, and I print out the object that is being bound, which looks as follows:



So I can see that the remote stub has been bound to the registry.

The error from the client on the other machine attempting to connect to my machine where I can see the object bound is:



Any ideas? I know it's quite close to being right (well I think it is anyway), but there must be a piece missing somewhere...

Many Thanks
Rob
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2531
    
    8

Hmm first check the server. Are you binding the RMI registry correctly on the server? If you can start your server independent of classpath (ie on 2 different computers) then the server shouldn't be the problem.

Then the client binding again is to make sure the "remote" object is the remote interface not the remote class. You probably used the remote class causing the ClassCastException.



Robert Wj Wilson
Greenhorn

Joined: May 26, 2009
Posts: 5
Hi K.Tsang, thanks for the quick reply!

I'm not sure the server is binding correctly on the other machine- On the local machine I can see the name that I used to bind it being listed, but when I start the server on the remote machine, I get the following output:



As you can see the the name is empty between the brackets. i bind my stub to the server using pretty much exactly the same code you have given in your example



And on the client side, again, almost the same code as you have posted, except that I get the registry with the Naming object:


when the client is run on the local machine to connect to the remote server, I get the following error



Which makes sense as the remote server didn't look like it was binding the remote object when it ran. Is there something I should be doing with the classpath of the server?

Sorry about the reems of code - I hope this explains how I've got things running and the problem I currently have. In short I think the server is not binding correctly, but I don't know why....

Many Thanks for all your help
Rob

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Rob,

If the following is true for both running your server and client on the same machine as running client and server on different machines, it would make sense client not being able to connect to the server (when client + server run on different machines)


because the server is not running on localhost, but on another pc, so you would expect "rmi://ip-address:1099/BodgitAndScarper"

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Howdy, Robert!

Champion, it looks like the code of your server is correct. I would only replace registry.rebind() by registry.bind().
Also, you can change the way you are getting a reference to your remote interface, just to see if it works. On the client side, you could have the following:



Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2531
    
    8

Ah Roel is right, can't use "localhost" for the rmi protocol. The IP address must be used in order for any computer to become the server. If localhost or 127.0.0.1 is used then the client must also be local.

I do recall that if you bind the server with localhost and you set up the client to connect to server with 127.0.0.1 it will not work and vice versa.
Robert Wj Wilson
Greenhorn

Joined: May 26, 2009
Posts: 5
Firstly I would like to thank you all for the fast responses and the help that you have given. I would also like to say that it is now working as i would expect across two different machines on the network! I'd also like to say sorry to Roel and K.Tsang as I led you up the garden path a bit with the local host comment that i had added to my client code - I accidentally wrote localhost as I had it on the brain as I was writing the post, but to clarify, I was using the machine name that the user entered to create the rmi address, so if the server was on "machine2" then the url I was constructing would have been "rmi://machine2:1099/BodgitAndScarper", so I can't apologise enough for that.

The thing that I changed that has made it work was to use Bob's suggestion of changing the look up code on the client from Naming.lookup(URL) to


I left the rebind statement in the server end - this may get changed with more testing.

That appears to be the code that allowed the client to work across the network, that and building the code up from the command line (in the right order, as mentioned in Andrew's book).

I have yet to try the server from another machine and the client from my local machine, so I may be back soon....

Thank you all for your help and advice and let's hope I don't have to come back any time soon!
Rob
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Hi Robert,

Glad to hear you could solve your problem.

I also used the code suggested by Roberto for retrieving a reference to the remote object, so that would have been my next suggestion. And if you do it like that, you also should not have the problem mentioned by K. Tsang with the "localhost" and the "127.0.0.1"

Kind regards,
Roel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RMI binding problems