Adding Code to a Client-Side File

RMI generates a client-side file with a main() method that looks 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. You can use the reference to invoke server-side methods.

To add a lookup() call:

  1. Make sure the RMI server program is running.

    You need to know which RMI registry the server is registered with. In a large development effort, the server might be under control of a system administrator or another developer. If you are doing all the development work, you can start the server yourself.

  2. Start the RMI Registry browser, specifying the registry in which the RMI server is registered.
  3. Build your RMI project and execute the remote object.

    A service node with the same name as the remote object appears under the registry node.

  4. Expand the service node and locate the subnode that represents the RMI server's remote interface.
  5. Right-click the remote interface node and choose Copy Client Code from the contextual menu.
  6. Return to your client class code in the Source Editor window, place the cursor underneath the line with the setSecurityManager() call, and paste the code from the clipboard.

    The main() method should resemble the code.

Adding Server Method Invocations

The object returned by the lookup is a proxy for the RMI server. You can invoke methods on it. Use code similar to the example:

obj.sayHello();

Your completed main method should look like this:

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();
}
See also
Generating a Client-side File
Compiling and Running a Client-side File

Legal Notices