Home : Course Map : Chapter 18 :
RMI Example Client
JavaTech
Course Map
Chapter 18

Introduction
Server Demo
Client Demo
Demo Support
Resources
Exercises

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The code for the client program RMIExampleClient described in the book is shown below. Note that it belongs to the package javatech.rmi18.client and so must reside in the client subdirectory as shown by this path:

javatech
  |
  |__ rmi18
       |
       |__ client

RMIExampleClient.java

// RMIExampleClient.java

package javatech.rmi18.client;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;

import javatech.rmi18.server.RMIExampleInterface;

/** A client that makes remote calls to the RMI server developed in
  * Chapter 18. **/
public class RMIExampleClient
{
  /** Constructor **/
  public RMIExampleClient () {

    // Create and install a security manager.
    if (System.getSecurityManager() == null) {
      System.setSecurityManager(new RMISecurityManager());
    }

    // Get a reference to the remote server named rmi-example-server.
    RMIExampleInterface rmi_example_server = null;
    try {
      // Find the server.
      String server_name = "//localhost:3001/rmi-example-server";
      System.err.println (
        "RMIExampleClient: looking up server " + server_name
      );
      rmi_example_server =
        (RMIExampleInterface) Naming.lookup (server_name);
      System.err.println ("RMIExampleClient: found it!");
    } // try
    catch (MalformedURLException mue) {
      System.err.println (mue);
    }
    catch (NotBoundException nbe) {
      System.err.println (nbe);
    }
    catch (RemoteException re) {
      System.err.println (re);
    }
    if (rmi_example_server == null) {
      System.err.println ("\nEXITING BECAUSE OF FAILURE");
      System.exit (1);
    }

    // Test method1.
    System.err.println (
      "Calling remote method1 which should echo the string" +
      " 'hello from client'"
    );
    try {
      rmi_example_server.method1 ("hello from client");
    }
    catch (RemoteException re) {
      System.err.println (re);
    }

    // Test the add method.
    try {
      int sum = rmi_example_server.add (18, 34);
      System.err.println (
        "According to the remote add(), the sum of 18 and 34 is " + sum
      );
    }
    catch (RemoteException re) {
      System.err.println (re);
    }
  } // ctor

  public static void main (String[] args) {
    new RMIExampleClient ();
  } // main
} // class RMIExampleClient

 

The following version is the same as above except for the server name.

RMIExampleClient.java.remote

// RMIExampleClient.java

package javatech.rmi18.client;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;

import javatech.rmi18.server.RMIExampleInterface;

/** A client that makes remote calls to the RMI server developed in
  * Chapter 18. **/
public class RMIExampleClient
{
  /** Constructor **/
  public RMIExampleClient () {

    // Create and install a security manager.
    if (System.getSecurityManager() == null) {
      System.setSecurityManager(new RMISecurityManager());
    }

    // Get a reference to the remote server named rmi-example-server.
    RMIExampleInterface rmi_example_server = null;
    try {
      // Find the server.
      String server_name = "//angel:3001/rmi-example-server";
      System.err.println (
        "RMIExampleClient: looking up server " + server_name
      );
      rmi_example_server =
        (RMIExampleInterface) Naming.lookup (server_name);
      System.err.println ("RMIExampleClient: found it!");
    } // try
    catch (MalformedURLException mue) {
      System.err.println (mue);
    }
    catch (NotBoundException nbe) {
      System.err.println (nbe);
    }
    catch (RemoteException re) {
      System.err.println (re);
    }
    if (rmi_example_server == null) {
      System.err.println ("\nEXITING BECAUSE OF FAILURE");
      System.exit (1);
    }

    // Test method1.
    System.err.println (
      "Calling remote method1 which should echo the string" +
      " 'hello from client'"
    );
    try {
      rmi_example_server.method1 ("hello from client");
    }
    catch (RemoteException re) {
      System.err.println (re);
    }

    // Test the add method.
    try {
      int sum = rmi_example_server.add (18, 34);
      System.err.println (
        "According to the remote add(), the sum of 18 and 34 is " + sum
      );
    }
    catch (RemoteException re) {
      System.err.println (re);
    }
  } // ctor

  public static void main (String[] args) {
    new RMIExampleClient ();
  } // main
} // class RMIExampleClient

 

See the RMI example support files page for the policy file plus the build and run command files for the client.

 

Most recent update: Oct.12.05

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.