Steps for developing RMI applications are as follows:
1. Defining the remote interface.
2. Implementing the remote interface.
3. Writing the code for registering the object.
4. Writing the client that uses the remote objects.
5. Generating stubs (client proxies) and skeletons (server entities).
6. Running the RMI Registry, server and client.

1. Defining the Remote Interface
An interface manifests the exposed operations and the client programmer need not be aware of the
implementation (the interface in this case also serves as a marker to the JVM). A remote Interface by definition
is the set of methods that can be invoked remotely by a client:

•  The remote interface must be declared public or the client will get an error when it tries to load a remote
    object that implements the remote interface.
•  The remote interface must extend the java.rmi.Remote interface.
•  Each method must throw a java.rmi.RemoteException (or a superclass of RemoteException)
•  If the remote methods have any remote objects as parameters or return types, they must be interface types
    not the implementation classes.

Note: java.rmi.Remote interface has no methods. It is just a marker interface.

Example: The example discussed in the following sections illustrates how to define and invoke methods on a
remote object.
We will define our Remote Interface (HelloInterface.java) as follows:

RMI Examples

2. Implementing the Remote Interface

RMI Examples

The implementing class is the actual class that provides the implementation for methods defined in the remote
interface. The java.rmi.server.RemoteObject extends the functionality provided by the java.lang.Object
class into the remote domain by overriding the equals(), hashcode() and toString() methods. The generic
java.rmi.server.RemoteObject is an abstract class and describes the behavior of remote objects.

The abstract subclass java.rmi.server.RemoteServer describes the behavior associated with the server
implementation and provides the basic semantics to support remote references.

java.rmi.RemoteServer has two concrete sub-classes
  
   • java.rmi.server.UnicastRemoteObject
      It designs a non-replicated remote object whose reference is valid only when the server is alive.
   java.rmi.activation.Activatable
      It is the concrete class that defines behavior for on demand instantiation of remote objects.

In addition to one of the above classes, the class corresponding to the remote interface must implement one or
more interfaces that define the remote methods. A remote class can define any methods but only methods in the
remote interface can be invoked remotely.

RMI Examples

3. Registering the Remote Object
Now that we have the interface and the implementation, we need to make this object available to clients by
binding it to a registry. This will allow the clients to look the object up on the host by a String name.

The stubs and skeletons (if any) are needed for registration. After all it is the object stub that is going to be
passed around from the registry to the clients.

RMI Examples

4. Writing the Client that uses the Remote Object
The client performs a lookup on the registry on the host and obtains a reference to the remote object. Note that
casting to the remote object is critical. In RMI, clients always interact with the interface, never with the object
implementation.

RMI Examples
RMI Examples

5. Generating Stubs and Skeletons
Now that we have the remote interface and implementation, we can generate the skeletons and stubs (or only
stubs in java 1.2 or higher version) with the rmic tool after we have compiled the classes. Remember to set the
directory that you are working from in your classpath; you can then use the following line in a command
window:

    rmic -v1.2 HelloServer


This will generate stub class “HelloServer_Stub.class”.

Note: It is worth keeping in mind that while starting the registry all classes and stubs must be available in the
classpath or the classpath should not be set at all, to support dynamic loading.

6. Running the RMI Registry, Client and the Server
The steps for running the RMI application are:

1. Start RMI Registry:
Ensure that the following classes are present in the current folder or the classpath:
    • HelloInterface.class
    • HelloServer_Stub.class

Start the RMI Registry by giving the command:
rmiregistry

2. Start Server:
Ensure that the following classes are in the current folder or the classpath:
    • HelloInterface.class
    • HelloServer.class
    • HelloServer_Stub.class
    • MyRMI.class

Run the MyRMI program.
D:sumit miin>java -Djava.rmi.server.codebase="file:///d:/sumit/rmi/bin/" org
.test.MyRMI

3. Run Client:
Ensure that the following classes are in the current folder or the classpath:
    • HelloInterface.class
    • HelloServer_Stub.class
    • HelloClient.class

Run the HelloClient program :)