Remote Procedure Call (RPC) in
Java
Using Java RMI (Remote Method
Invocation)
What is RPC?
• • RPC (Remote Procedure Call) allows a
program to execute code on a remote server
as if it were local.
• • It enables distributed computing by
abstracting network communication.
• • Java provides built-in support for RPC using
RMI (Remote Method Invocation).
Steps to Implement Java RMI
• 1. Define a remote interface.
• 2. Implement the remote interface.
• 3. Create an RMI server.
• 4. Create an RMI client.
• 5. Run the RMI application.
Step 1: Define a Remote Interface
• public interface Calculator extends Remote {
• int add(int a, int b) throws
RemoteException;
• }
Step 3: Create the RMI Server
• public class RMIServer {
• public static void main(String[] args) {
• LocateRegistry.createRegistry(1099);
• Calculator calculator = new CalculatorImpl();
•
Naming.rebind("rmi://localhost/CalculatorService",
calculator);
• System.out.println("RMI Server is running...");
• }
• }
Step 4: Create the RMI Client
• public class RMIClient {
• public static void main(String[] args) {
• Calculator calculator = (Calculator)
Naming.lookup("rmi://localhost/CalculatorServic
e");
• int result = calculator.add(5, 3);
• System.out.println("Result: " + result);
• }
• }
Running the RMI Application
• 1. Compile all Java files:
• javac *.java
• 2. Start the RMI Registry:
• rmiregistry 1099 &
• 3. Run the RMI Server:
• java RMIServer
• 4. Run the RMI Client:
• java RMIClient