Skip to content

Java Programming

Software Development | Java RMI

Basic Model of a JAVA RMI: Execution of distributed objects in Java is possible with the help of Java RMI. A client can access a Java Object Host which is on a remote virtual machine from any JVM. It is a two step procedure. The first step is about execution of the clients and servers in Java. This will allow them to have an inherent object-oriented appearance and objects will interact with all the characteristics of Java. So it means that we can access/run a server object from any java virtual machine (JVM) therefore, we can achieve the platform independence.

Second is that the Java RMI’s fundamental model always has a client program, and this client program is used to approach the remote objects from any Java virtual machine. The connection between a client and remote objects need to reference an object, which is hosted by a server program. Remote server objects can be located by a server in two different ways. These two procedures have their own methods to give remote reference to the client. These procedures are,

• Explicitly.

• Implicitly.

Both are used for “obtaining a remote reference”.

Java RMI Architecture:

To create a Math service using Java RMI I have followed these steps.

1. Define a remote interface

2. Implementation of the server

3. Implementation of the client

4. Compile the source code

5. Start Java RMI registry, server and then client.

Create the Remote Interface:

In Java RMI an interface is used to extend the “java.rmi.Remote” interface. Remote interface doesn’t have any method of its own, and it is used for tagging the remote objects, which makes possible to identify as it is. (Harold E. R., 2000). Set of remote methods also declared in the interface. Every single remote method must declare “java.rmi.RemoteException” or a superclass of the “RemoteException” in its throws section, in addition to any application’s specific exception.

Example which I have used in this paragraph for remote interface, “bite.example.SampleServer”. It declares the four methods, “Addition”, “Subtraction”, “Multiplication” and “Square”.

Following is the source code for “”.

Sample Server

package bite.example;

import java.rmi.Remote;

import java.rmi.RemoteException;

public interface SampleServer extends Remote


public int addition(int x, int y) throws RemoteException;

public int subtract(int x, int y)throws RemoteException;

public int multiply(int x, int y)throws RemoteException;

public int square(int x)throws RemoteException;

public int getValue()throws RemoteException;

public String getLastOp()throws RemoteException;


Implementation of Sever:

In this context our “server” class exports the remote object and this server class holds a “main” method, this produce an instance of the remote object implementation. Then it combines that instance to a name in a Java RMI registry. The class which holds this “main” method possibly the implementation class by itself or another class completely.

In the class “Server” we declare the “main” method for server, and it also perform the remote interface SampleServer. Our server’s “main” method follows these two steps. A new remote object is produced and export in first step, second step is about the registration of an object with a Java RMI registry.

Source code for class “” is as following.

Sample Server Impl.

package bite.example;

import java.rmi.registry.Registry;

import java.rmi.registry.LocateRegistry;

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

public class SampleServerImpl implements SampleServer {

private int value =0;

private String lastOp;



public int addition(int x, int y) throws RemoteException {

value = x + y;

lastOp = “ADDITION”;

return value;


public int subtract(int x, int y)throws RemoteException{

value = x – y;


return value;


public int multiply(int x, int y)throws RemoteException{

value = x*y;

lastOp = “MULTIPLY”;

return value;


public int square(int x)throws RemoteException{

value = x*x;

lastOp = “SQUARE”;

return value;


/* Resource properties */

public int getValue() throws RemoteException {

return value;


public void setValue(int value) {

this.value = value;


public String getLastOp()throws RemoteException {

return lastOp;


public void setLastOp(String lastOp) {

this.lastOp = lastOp;


public static void main(String args[]) {

try {

//Create and export a remote object

SampleServerImpl obj = new SampleServerImpl();

SampleServer stub = (SampleServer)UnicastRemoteObject.exportObject(obj, 0);

//Register the remote object with a Java RMI registry

//and bind the remote object’s stub in the registry

Registry registry = LocateRegistry.getRegistry();

registry.bind(“SampleServer”, stub);

System.err.println(“Server ready”);

} catch (Exception e) {

System.err.println(“Server exception: ” + e.toString());





Implementation of Client:

Client class acquires a “stub” for the registry on the server’s host, and it is searches remote object’s stub in the registry by their names, and then it invokes the “Addition”, “Subtraction”, “Multiply” and “Square” methods on the remote object using stub.

Source code for the Client is following.

Sample Client

package bite.example;


import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

import java.util.Scanner;

public class SampleClient {

public static void main(String[] args) {

String host = (args.length start java -Djava.rmi.server.codebase=file:C:/rmi/ bite.example.SampleServerImpl”

And then I have got the output “Server ready”

Start the Client: Final step is to start the client. When server was ready I open another window of command prompt line and then run the client as follow “C:rmi>java bite.example.SampleClient”

Be Sociable, Share!
    The following two tabs change content below.

    One Comment (Add Yours)

    1. syntax highlighting for code blocks can be nice.

    Add Your Comment (Get a Gravatar)