Nearby lessons
80 of 125Java - RMI (Remote Method Invocation)
- 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.
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
| Part | Role |
|---|---|
| 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 Registry | A phonebook. The server 'binds' (registers) its remote object here; the client 'looks up' the object here to get the stub. |
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.
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.
The Four Steps to Build an RMI Application
Step 3 — Write the server that registers the object
The Four Steps to Build an RMI Application
Step 4 — Write the client that calls the remote method
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.
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.
| Point | RMI | REST (modern) |
|---|---|---|
| Data format | Java objects only (serialized) | JSON/XML (any language can read) |
| Language | Java-to-Java only | Works with any language |
| Transport | Custom RMI protocol (port 1099) | Standard HTTP (port 80/443) |
| Firewall friendly? | Often blocked | Yes — HTTP passes everywhere |
| When used | Legacy distributed systems | Today's web and mobile apps |
Marshalling and Unmarshalling — How the Call Travels
The classic material explains the journey of a remote method call in detail. Two important words:
| Term | Meaning |
|---|---|
| Marshalling | Converting data + method call into a form that can travel over the network (in RMI, this uses serialization). |
| Unmarshalling | Converting the received bytes back into a usable object on the other side (deserialization). |
The full journey of obj.add(2, 3) across the network:
- 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.