Skip to content
Related Articles
Open in App
Not now

Related Articles

Calculator Using RMI(Remote Method Invocation) in Java

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 31 Aug, 2022
Improve Article
Save Article

RMI (Remote Method Invocation) is an API used to access objects running on another JVM(Server-side). It is mainly used for the creation of distributed systems and is provided in Java Rome. Stub and Skeleton are the two objects used for handling communication between client and server. The following figure shows an overview of RMI.

Working of RMI

Here,

  • Stub Object: The stub object on the client machine builds an information block and sends this information to the server.
  • Skeleton Object: The skeleton object passes the request from the stub object to the remote object. RMI contains a rmiregistry that holds all the server objects. The server binds all the objects to the registry and then the client fetches the object from the respective registry after which the client invokes the methods using the fetched objects.

Steps to create Calculator using RMI 

Step 1: Create the Remote interface

First, we will create 4 interfaces(addition, subtraction, multiplication, division). These interfaces are helpful for the operation. To create remote interfaces we need to extend the remote interface and the method prototype within the interface should throw the RemoteException.

AddI.java

 
SubI.java

 
MulI.java

 
DivI.java

 

Step 2: Implementation of the remote interface

Now it is time to provide implementation to all the interfaces. To implement the remote interface, the class should extend to the UnicastRemoteObject class of the java.rmi package. Also, a default constructor needs to be created to throw the java.rmi.RemoteException from its parent constructor in the class.

Impl.java

 

Step 3: Create and execute the server application program.

The next step is to create the server application program and execute it on a separate command prompt. The rebind method of the Naming class is used to bind the remote object to the new name.

Server.java

 

Step 4: Create and execute the client application program.

The next step is to create the client application program and execute it on a separate command prompt. The lookup method of Naming class is used to get the reference of the Stub object.

Client.java

 

Step 5: Compile all the java program

Now we need to compile all the java program. To compile all the java program we need to open the command prompt and enter into the respective folder. Now enter into the folder where all the files are stored. We can compile all file at a time by using the following command;

javac *.java

Step 6: Create a stub and skeleton

The rmic tool is used to invoke the rmi compiler that creates the Stub and Skeleton objects. Its prototype is:

rmic classname

Step: 7 Start the registry service by the rmiregistry tool

Now start the rmi registry service by using rmiregistry tool. We need to specify port number. If we don’t specify the port number, it uses a default port number for example we are using port number 5259.

rmiregistry 5259  or  rmiregistry &  or start rmiregistry(windows)

After doing the above steps properly it may look like this:

Calculator using rmi in java - rmiregistry tool

Output: After successfully following the above steps you can see the following output or any confusion in the above steps you can watch the following video

My Personal Notes arrow_drop_up
Next
Related Articles
Article Contributed By :
Vote for difficulty
Current difficulty : Hard
Article Tags :
Practice Tags :

Start Your Coding Journey Now!