RMI (Remote Method Invocation) is a Java mechanism that allows an object in one Java Virtual Machine (JVM) to invoke methods of an object in another JVM over a network.
👉 In simple words:
RMI allows Java programs to call methods on remote computers as if they were local.
📊 Concept Diagram (Description) Client JVM → Stub → Network → Skeleton → Server JVM
📊 Flow:
Client → Stub → Remote Reference Layer → Skeleton → Remote Object → Response back
import java.rmi.*;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
👉 Must extend Remote
👉 Methods must throw RemoteException
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
public MyRemoteImpl() throws RemoteException {
super();
}
public String sayHello() {
return "Hello from Server";
}
}
import java.rmi.*;
public class Server {
public static void main(String[] args) {
try {
MyRemoteImpl obj = new MyRemoteImpl();
Naming.rebind("rmi://localhost/hello", obj);
System.out.println("Server started...");
} catch(Exception e) {
System.out.println(e);
}
}
}
import java.rmi.*;
public class Client {
public static void main(String[] args) {
try {
MyRemote obj = (MyRemote) Naming.lookup("rmi://localhost/hello");
System.out.println(obj.sayHello());
} catch(Exception e) {
System.out.println(e);
}
}
}
| Class | Purpose |
|---|---|
| Remote | Marker interface |
| RemoteException | Handles network errors |
| Naming | Registry lookup/binding |
| UnicastRemoteObject | Remote object support |
👉 A registry service that stores remote objects.
Commands:
rmiregistry
| Feature | RMI | Socket |
|---|---|---|
| Level | High-level | Low-level |
| Complexity | Easy | Complex |
| Communication | Method-based | Data-based |
| Language | Java only | Any language |
RemoteRemoteExceptionNaming.lookup() in client📊 RMI Flow Diagram:
Client → Stub → Network → Skeleton → Remote Object → Response
RMI = Remote Method Invocation
Calls methods across JVMs
Components:
Uses registry for lookup
Java-to-Java communication
Open this section to load past papers