Completing Client-side Files

The RMI module generates client-side files with main() methods that look like the following example:

public static void main (String[] args) {
   System.setSecurityManager (new RMISecurityManager());
}

To complete your client class, you must add a lookup() call that returns a reference to the correct RMI server. Then you can use the reference to invoke server-side methods.

Adding the lookup() Call

  1. The RMI server program should be running. You need to know which RMI registry it is registered with. In a large development effort, the server may be under control of a system administrator or another developer. If you are doing all of the development work you can start the server yourself.
  2. Start the RMI Module's Registry Browser, specifying the registry in which the RMI server is registered.
  3. Open the Registry Browser's RMI Registry node. Click through the node hierarchy and locate the subnode that represents the RMI server's remote interface.
  4. Right-click on the remote interface node. This opens a context menu; select Copy Client Code.
  5. Return to your client class code in the IDE's editing window.
  6. Place the cursor underneath the line with the setSecurityManager() call. Paste the code from the clipboard. After you paste, the main() method should resemble the following example:
    1. public static void main (String[] args) {
         System.setSecurityManager (new RMISecurityManager());
         try {
            Hello obj = (Hello) Naming.lookup ("//localhost:1099/Remote");
         }
         catch (Exception ex) {
            ex.printStackTrace();
         }
      }
Adding Server Method Invocations
  1. The object returned by the lookup is a proxy for the RMI server. You can invoke methods on it.
  2. Use code like this:
    1. obj.sayHello();
  3. Your completed main method will look like this:
    1. public static void main (String[] args) {
         System.setSecurityManager (new RMISecurityManager());
         try{
            Hello obj = (Hello) Naming.lookup ("//localhost:1099/Remote");
            obj.sayHello();
         }
         catch (Exception ex)
         {
            ex.printStackTrace();
         }

Compiling and Running Your Client

  1. The RMI server must be running before you can successfully run your client.
  2. In the Explorer Window's Filesystems tab, right-click on your client class and open its property sheet. Make sure that the Executor property is set to RMI Client Execution.
  3. Right-click on the node for the package that contains your client and choose Compile from the context menu.
  4. Right-click again on the node for the client class and choose Execute from the context menu.
For more information see:
Legal Notices