Calculator Using RMI(Remote Method Invocation) in Java

10 Sep, 2021
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.
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
// Creating a AddInterface interfaceimport java.rmi.Remote;public interface AddInterface extends Remote {    // Declaring the method prototype    public int add(int x, int y) throws RemoteException;}
SubI.java
// Creating a SubInterface interfaceimport java.rmi.Remote;public interface SubInterface extends Remote {    // Declaring the method prototype    public int sub(int x, int y) throws RemoteException;}
MulI.java
// Creating a MulInterface interfaceimport java.rmi.Remote;public interface MulInterface extends Remote {    // Declaring the method prototype    public int mul(int x, int y) throws RemoteException;}
DivI.java
// Creating a DivInterface interfaceimport java.rmi.Remote;public interface DivInterface extends Remote {    // Declaring the method prototype    public int div(int x, int y) throws RemoteException;}
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
// Java program to implement the AddInterface,// subInterface, MulInterface, and DivInterfaceimport java.rmi.*;import java.rmi.server.*;  public class Impl extends UnicastRemoteObject    implements AddInterface, SubInterface, MulInterface,               DivInterface {      // Default constructor to throw RemoteException    // from its parent constructor    public Impl() throws Exception { super(); }      // Implementation of the AddInterface,    // subInterface, MulInterface, and DivInterface    public int add(int x, int y) { return x + y; }    public int sub(int x, int y) { return x - y; }    public int mul(int x, int y) { return x * y; }    public int div(int x, int y) { return x / y; }}
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
// Program for server applicationimport java.rmi.*;import java.rmi.registry.*;public class Server {    public static void main(String[] args) throws Exception    {          // Create an object of the interface        // implementation class        Impl obj = new Impl();          // Binds the remote object by the name ADD        Naming.rebind("ADD", obj);          System.out.println("Server Started");    }}
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
// Program for client applicationimport java.rmi.*;import java.util.*;public class Client {    public static void main(String[] args) throws Exception    {        Scanner sc = new Scanner(System.in);        while (true) {            // User Menu            System.out.println(                "\n1.Addition\n2.Subtraction\n3.multiplication\n4.division\n5.Exit");            System.out.println("Enter the option:");            int opt = sc.nextInt();            if (opt == 5) {                break;            }            System.out.println(                "Enter the the first number:");            int a = sc.nextInt();            System.out.println("Enter the second number:");            int b = sc.nextInt();            int n;            switch (opt) {            case 1:                // lookup method to find reference of remote                // object                AddInterface obj                    = (AddInterface)Naming.lookup("ADD");                n = obj.add(a, b);                System.out.println("Addition= " + n);                break;            case 2:                SubInterface obj1                    = (SubInterface)Naming.lookup("ADD");                n = obj1.sub(a, b);                System.out.println("Subtraction= " + n);                break;            case 3:                MulInterface obj2                    = (MulInterface)Naming.lookup("ADD");                n = obj2.mul(a, b);                System.out.println("Multiplication = " + n);                break;            case 4:                DivInterface obj3                    = (DivInterface)Naming.lookup("ADD");                n = obj3.div(a, b);                System.out.println("Division = " + n);                break;            }        }    }}
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:
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
sweetyty
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Multidimensional Arrays in Java
Collections in Java
Set in Java
Singleton Class in Java
Initializing a List in Java