Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

LD220

macrumors member
Original poster
Jun 3, 2009
35
0
Hello. I'm new to Java RMI and I'm having some trouble. My professor had us copy this sample code that he wrote very quickly in class and said that it should run if we first compile the code with javac. Then start the rmiregistry and do "java RMI_first_demo_Server <port#>" in one terminal, and in a separate terminal do "java RMI_first_demo_Client <String> localhost" in another terminal. The server side seems to run fine for me, but then when I start the client I'm getting the error message we created "RMI_first_demo Naming lookup fails!." What am I missing here? I've uploaded the code. Thanks for your help!
 

Attachments

  • RMI_first_demo_Client.txt
    1.1 KB · Views: 119
  • RMI_first_demo_Interface.txt
    221 bytes · Views: 123
  • RMI_first_demo_Server.txt
    1.6 KB · Views: 171
Your professor is probably trying to see if you can debug the application.

Can you think of a way of getting more information from the part of the client code where it prints "RMI_first_demo Naming lookup fails!." (hint: it's in a "catch (Exception e)" block)

Can you think of a reason why the lookup would fail in the client?

Hint: read the Java docs for java.rmi.Naming lookup method : http://java.sun.com/javase/6/docs/api/java/rmi/Naming.html#lookup(java.lang.String)
 
Thanks. I'll take a look and see if I can figure it out. He's not looking for us to debug the code on our own. He was just giving us a demo of what the code could do. When I asked him why it didn't work he said I might have to include a port number with localhost, but he wasn't sure (not very helpful). I didn't think that was right though, so I've just been googling, trying to figure out what's going on because he didn't really give us an explanation of RMI, he just gave us this code. Any help would be great.
 
Ok, it's a good exercise for you to debug this. No you don't need to specify a port with localhost.

Try to find out what the Exception is. Do you know how to print the exception stack trace?

Then look up the Javadocs to see why lookup would throw that particular exception.

Then relate what the server is using to bind to the rmiregistry to what the client is using to look up the server.
 
I've thought about this a bit more and I think you need a refresher on RMI. Think about what RMI means ... it's "Remote Method Invocation". In other words, you have a client that's attempting to call a method that's provided by another process. So how does the client know what to call?

That's where the rmiregistry comes in. The server (remote) process registers itself with rmiregistry using a name. Clients that want to do RMI need to get an underlying reference to the remote process, but the only place it can get this from is rmiregistry, so they need to query rmiregistry using the same name that the server registered itself with. This is where the call to java.rmi.Naming.lookup comes in.

Now look in your professor's code and try to find where this process is failing.

One way to avoid this in production code would be to define the remote process's name as a constant in the interface, ie :
Code:
public interface RMI_first_demo_Interface extends java.rmi.Remote {
    static public final String SERVICE_NAME = "....";

    public void greeting(String message) throws RemoteException;
        
}
 
Thanks so much for your reply. I found the error. It's pretty silly looking at it now, but at least it's up and running. I really appreciate it.

By the way, I laughed a little when you said I need an RMI refresher, cause that implies that my professor actually taught us something...this was literally all he gave us.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.