RMI - The first example

As stated by Sun Microsystems in its documentation , "The goals for supporting distributed objects in the Java language are:

Support seamless remote invocation on objects in different virtual machines. Support callbacks from servers to applets. Integrate the distributed object model into the Java language in a natural way while retaining most of the Java language's object semantics. Make differences between the distributed object model and local Java object model apparent. Make writing reliable distributed applications as simple as possible. Preserve the safety provided by the Java runtime environment."

Quoting again from Sun's Remote Method Invocation Specification:

In the Java distributed object model, a remote object is one whose methods can be invoked from another Java Virtual Machine, potentially on a different host. An object of this type is described by one or more remote interfaces, which are Java interfaces that declare the methods of the remote object.

Remote method invocation (RMI) is the action of invoking a method of a remote interface on a remote object. Most importantly, a method invocation on a remote object has the same syntax as a method invocation on a local object.

The Distributed and Non-Distributed Models Contrasted

The Java distributed object model is similar to the Java object model in the following ways:

The Java distributed object model differs from the Java object model in these ways:

A Simple Example

As usual, the first example that we will look at is "Hello World." Unfortunately we will have to immediately deal with many of the complexities of RMI.

The following diagram provides a time-line for interactions and events necessary to write

Hello World!

Here is the source code:

file: Hello.java

import java.rmi.*;
public interface Hello extends java.rmi.Remote {
	String sayHello() throws java.rmi.RemoteException;

file: HelloMain.java

import java.awt.*;
import java.rmi.*;

public class HelloMain {
	String message = "";
	public static void main(String[] args ) {
     	// Create and install a security manager
		System.setSecurityManager(new RMISecurityManager());
		try {
			Hello obj = (Hello)Naming.lookup("//jinx.umsl.edu/~siegel/myRMI/HelloServer");
			System.out.println( obj.sayHello());
		    } catch (Exception e) {
			System.out.println("HelloApplet exception:"+ e.getMessage());

file: HelloImpl.java

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class HelloImpl
	extends UnicastRemoteObject
	implements Hello
	private String name;

	public HelloImpl(String s) throws RemoteException {
		name = s;

	public String sayHello() throws RemoteException {
		return  "Hello World!";
public static void main(String args[])
		// Create and install a security manager
		System.setSecurityManager(new RMISecurityManager());

		try {
			HelloImpl obj = new HelloImpl("HelloServer");
			Naming.rebind("//jinx.umsl.edu/~siegel/myRMI/HelloServer", obj);
			System.out.println("HelloServer bound in registry");
		} catch (Exception e) {
			System.out.println("HelloImpl err: " + e.getMessage());

Building and Registering HelloImpl

I will work in a directory "myRMI" in my public_html directory. Since references will be through URLs we need to be in public_html

  1. Begin by placing HelloImpl.java in myRMI and compile with javac.

  2. Next, execute rmic HelloImpl. The myRMI directory now contains the files:
    Communications between systems is actually accomplished between the "Skel" on the server and the "Stub" on the client.

  3. Start the rmiregistry with rmiregistry &. (Run it in the background)

  4. Create a "java.policy file and put it in the same directory. Here is mine, yours can be the same:

    grant {
        permission java.net.SocketPermission "*:1024-65535","connect,accept";
        permission java.net.SocketPermission "*:80" , "connect" ;

  5. Finally run HelloImpl. The command we need is

    java -Djava.rmi.server.codebase=http://jinx.umsl.edu/~siegel/myRMI -Djava.rmi.server.hostname=jinx.umsl.edu -Djava.security.policy=java.policy HelloImpl &

    the message that we get back is: HelloServer bound in registry.

Building and Running HelloMain

I will run it from my Windows 2000 laptop:

  1. Place HelloMain.java and Hello.java in a convienient directory and compile them with javac

  2. Move HelloImpl_Stub.class and java.policy into the same directory.

  3. Finaly run:

    java -Djava.rmi.server.codebase=http://jinx.umsl.edu/~siegel/myRMI -Djava.rmi.server.hostname=jinx.umsl.edu -Djava.security.policy=java.policy HelloMain

  4. We get back, Hello World!.