Creating an RMI Client

It's extremely easy to create an RMI client. You just need to use the java.rmi.Naming class to locate the remote object in the RMI Registry. Listing 4.6 shows a client that accesses the MOTDImpl server.

Code Listing 4.6. Source Code for MOTDClient.java
package usingj2ee.motd;

import java.rmi.*;

public class MOTDClient
{
    public static void main(String[] args)
    {
        try
        {
// Locate the remote object
            MOTD motd = (MOTD) Naming.lookup("//localhost/MessageOfTheDay");

// Invoke the remote method and print the result
            System.out.println(motd.getMOTD());
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
}
					

Unlike the server, an RMI client doesn't need to use any special security features. You don't need to see a policy file or install ...

Get Special Edition Using Java™ 2 Enterprise Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.