To develop an RMI-based application you need to follow these steps:
  1. Create an interface that defines the exported methods that the remote object implements (that is, the methods that the server implements and that clients can invoke remotely). This interface extends the java.rmi.Remote interface.

    Each method in the interface must be declared to throw a java.rmi.RemoteException which is the superclass of many more specific RMI exception classes.

  2. Define a subclass of java.rmi.server.UnicastRemoteObject that implements your Remote interface. This class represents the remote object (or server object). Other than declaring its remote methods to throw RemoteException objects, the remote object does not need to do anything special to allow its methods to be invoked remotely. The UnicastRemoteObject and the rest of the RMI infrastructure handle this automatically.

  3. Write a program (a 'server') that creates an instance of your remote object.

    Export the object, making it available for use by clients, by registering the object by name with a registry service.

    This is usually done with the java.rmi.Naming class and the rmiregistry program.

    A server program may also act as its own registry server by using the LocateRegistry class and registry interface of the java.rmi.registry package.

  4. After you compile the server program (with javac) use rmic to generate a 'stub' and 'skeleton' for the remote object. Invoke rmic with the name of the remote object class (not the interface) on the command line. It creates and compiles two new classes with the suffixes _Stub and _Skel.

    Note: with RMI the client and the server do not communicate directly. On the client side the client's reference to a remote object is implemented as an instance of a 'stub' class. When the client invokes a remote method, it is a method of this stub object that is actually called. The stub does the necessary networking to pass the invocation to a 'skeleton' class on the server. This skeleton translates the networked request into a method invocation on the server object, and passes the returned value back to the stub, which passes it back to the client. This can be a complicated system but fortunately application programmers never have to think about stubs and skeletons; they are generated automatically by the rmic tool.

  5. If the server uses the default registry service provided by the Naming class you must run the registry server, if it is not already running.

    You can run the registry server by invoking the rmiregistry program.

    Note however that, as we mentioned at step 3 a server program may also act as its own registry server by using the LocateRegistry class and registry interface of the java.rmi.registry package so you need to run the registry server (or make sure it is running) only if the server uses the default registry service provided by the Naming class.

    In our two examples we use the default registry service.

  6. Now you can write a client program to use the remote object exported by the server.

    Notes:

    The client must first obtain a reference to the remote object exported by the server by using the Naming class to look up the object by name.

    The name is typically an rmi: URL.

    The remote remote reference that is returned is an instance of the Remote interface for the object (or more specifically a 'stub' object for the remote object). Once this client has this remote object it can invoke methods on it exactly as it would invoke the methods of a local object.

    The only thing that it must be aware of is that all remote methods can throw RemoteException objects, and that in the presence of network errors, this can happen at unexpected times.

    RMI uses the serializaton mechanism to transfer the stub object from the server to the client. Because the client may load an untrusted stub object, it should have a security manager installed to prevent a malicious (or just buggy) stub from deleting files or otherwise causing harm. The RMISecurityManager class is a suitable security manager that all RMI clients should install.

  7. Finally, start up the server program, and run the client!