Calculator Using RMI(Remote Method Invocation) in Java
Last Updated :
31 Aug, 2022
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.
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.
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
[tabbyending]
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
[tabbyending]
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
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read