Nearby lessons

80 of 125

Java - RMI (Remote Method Invocation)

📌 What You Will Learn
  • What RMI means in simple words
  • The RMI architecture: stub, skeleton, registry
  • The step-by-step way to build an RMI application
  • How RMI compares to modern REST web services

RMI (Remote Method Invocation) is a core concept of the Java language. This lesson explains What is RMI?, The RMI Architecture — Three Heroes and The Four Steps to Build an RMI Application with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

What is RMI?

RMI (Remote Method Invocation) lets a program on one computer call a method on an object running on another computer, just as if that object were on the same machine.

In simple words: RMI lets your program call a method that runs on another computer exactly as if it were a local object. Java silently packs the call, sends it over the network, and brings back the answer.

Simple example: your computer (client) wants to add two numbers, but the calculation logic lives on a server computer. With RMI, you simply call serverObj.add(10, 20) and Java secretly sends the request over the network and brings back the answer 30.

This is called distributed computing — the work is spread over more than one machine.

The RMI Architecture — Three Heroes

PartRole
Stub (client side)A local stand-in for the remote object. The client calls the stub; the stub sends the request over the network.
Skeleton (server side)Receives the request, calls the real method on the real object, and sends back the result.
RMI RegistryA phonebook. The server 'binds' (registers) its remote object here; the client 'looks up' the object here to get the stub.
In simple words: The stub is a fake object on the client, the real object lives on the server, and the registry is the phonebook that connects them. The client never talks to the server directly — it only talks to the stub.
Trainer's Note: Fun fact: modern Java no longer needs the skeleton class (Java 1.2 removed it). The remote method dispatch is handled automatically. But the idea — stub on client, real object on server — is still exactly how RMI works.
Example02
JCode Cell
1CLIENT COMPUTER SERVER COMPUTER
2 
3Client --> Stub --(network)--> Skeleton --> Real Object
4 ^ |
5 |---------(result back)-------------+
6 
7RMI Registry = phonebook where the server registers the object

The Four Steps to Build an RMI Application

Any RMI application follows the same four steps. Let us build a remote calculator step by step.

Step 1 — Create a remote interface (extends Remote)

The interface declares which methods can be called from another machine. It must extend java.rmi.Remote, and every method must declare throws RemoteException.

In simple words: A remote interface is just the menu of methods other machines are allowed to call. It must extend Remote, and every method must say throws RemoteException because network calls can fail at any time.

Step 2 — Implement the remote interface (extends UnicastRemoteObject)

The implementation class gives the real body of the methods. It extends UnicastRemoteObject to become an object that can be called over the network.

Example03
JCode Cell
1import java.rmi.*;
2 
3public interface CalcRemote extends Remote {
4 int add(int a, int b) throws RemoteException;
5 int multiply(int a, int b) throws RemoteException;
6}

The Four Steps to Build an RMI Application

Step 3 — Write the server that registers the object

Example04
JCode Cell
1import java.rmi.*;
2import java.rmi.server.UnicastRemoteObject;
3 
4public class CalcImpl extends UnicastRemoteObject implements CalcRemote {
5 public CalcImpl() throws RemoteException { } // constructor must allow
6 
7 public int add(int a, int b) { return a + b; }
8 public int multiply(int a, int b) { return a * b; }
9}

The Four Steps to Build an RMI Application

Step 4 — Write the client that calls the remote method

Example05
JCode Cell
1import java.rmi.registry.*;
2 
3public class CalcServer {
4 public static void main(String[] args) throws Exception {
5 CalcImpl obj = new CalcImpl();
6 Registry reg = LocateRegistry.createRegistry(1099); // start registry
7 reg.rebind("CalcService", obj); // register object
8 System.out.println("Server ready on port 1099");
9 }
10}

The Four Steps to Build an RMI Application

Compile and run order: compile all files, then start registry + server first, then the client. The client receives the stub from the registry and calls methods on it as if it were local.

Example06
JCode Cell
1import java.rmi.registry.*;
2 
3public class CalcClient {
4 public static void main(String[] args) throws Exception {
5 Registry reg = LocateRegistry.getRegistry("localhost", 1099);
6 CalcRemote obj = (CalcRemote) reg.lookup("CalcService"); // get the stub
7 
8 System.out.println("2 + 3 = " + obj.add(2, 3));
9 System.out.println("2 x 3 = " + obj.multiply(2, 3));
10 }
11}
Output
2 + 3 = 5 2 x 3 = 6

RMI vs Modern REST Web Services

RMI was the star technology of the 1990s and early 2000s for distributed Java applications. Today, most systems use REST web services over HTTP.

PointRMIREST (modern)
Data formatJava objects only (serialized)JSON/XML (any language can read)
LanguageJava-to-Java onlyWorks with any language
TransportCustom RMI protocol (port 1099)Standard HTTP (port 80/443)
Firewall friendly?Often blockedYes — HTTP passes everywhere
When usedLegacy distributed systemsToday's web and mobile apps
Trainer's Note: Trainer's honest advice: you will rarely write fresh RMI code in a modern job — REST APIs have replaced it. But RMI is still in many university syllabuses and interviews, and its core ideas (remote object, registry, stub, serialization over the network) are exactly the ideas behind modern micro-services. Learn it once, and web services become easy.

Marshalling and Unmarshalling — How the Call Travels

The classic material explains the journey of a remote method call in detail. Two important words:

TermMeaning
MarshallingConverting data + method call into a form that can travel over the network (in RMI, this uses serialization).
UnmarshallingConverting the received bytes back into a usable object on the other side (deserialization).
In simple words: Marshalling packs data and a method call into bytes for the journey; unmarshalling unpacks the bytes at the other end. In RMI both use Java serialization under the hood.

The full journey of obj.add(2, 3) across the network:

Example08
JCode Cell
1CLIENT NETWORK SERVER
2 
3Client calls obj.add(2,3)
4 |
5 v
6 Stub --marshals + serializes the call-->
7 Skeleton --unmarshals + deserializes-->
8 Real object computes 5
9 <-- result comes back the same way -- |
10 result = 5
📝 Key Takeaways
  • RMI lets a program call a method on an object living on another computer.
  • Stub (client) sends requests; the real object (server) does the work; the registry connects them.
  • Remote interface extends Remote; methods throw RemoteException.
  • Implementation extends UnicastRemoteObject.
  • Server registers with rebind(); client finds it with lookup().
  • Modern systems prefer REST over HTTP, but RMI teaches the same distributed-thinking concepts.

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8