Java RMI

Java RMI

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

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. For the Connection between a client and a remote object need a reference to an object, is hosted by a server program. Remote server objects can be located by the server in two different ways. These two procedures have their methods to give remote reference to the client. These are 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 to follow 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 a client.

Create the Remote Interface:

In Java RMI an interface is used to extend the “java.rmi.Remote” interface. The 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). A 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 an application’s specific exception.

An 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 hold a “main” method, this produces 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 performs the remote interface SampleServer. Our server’s “main” method follows these two steps. A new remote object is produced and export in the first step, the second step is about the registration of an object with a Java RMI registry.

The source code for class “” is as follows.

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 laptop;



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 the Client:

Client class acquires a “stub” for the registry on the server’s host, and it is searching 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.

The source code for the Client is the 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: The final step is to start the client. When the server was ready I open another window of command prompt line and then run the client as following “C:rmi>java bite.example.SampleClient”

Source by Muhammad Tanveer

Leave a Reply

Your email address will not be published.