4.4.3 The Client

In creating the client, we simply need to query the remote rmiregistry to receive an object reference to the object we're after, again using the java.rmi.Naming class. Once we have a reference to the remote object, of type given by the remote interface, we can then invoke the methods as though they were local.

Create a file called UserIdClient.java, again substituting for UserId as appropriate.

import java.rmi.*;
 
public class IanwClient  {
 
    private static void usage() {
        System.err.println("usage: java IanwClient hostname");
    }
 
    public static void main(String args[]) {
        try {
            if(args.length != 1) {
            usage();
            System.exit(-1);
            }
 
 
            String name = "//" + args[0] + "/" + Ianw.rmiName;
            Ianw server = (Ianw) Naming.lookup(name);
            System.out.println(server.sayHello());
 
        } catch (Exception e) {
            System.err.println("ChatClientImpl exception " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Ian Wakeman 2005-02-22