Skip to content
Home ยป Compiling and executing the server and the client

Compiling and executing the server and the client

Below is a clear, structured, and detailed explanation of RMI: Compiling and Executing the Server and Client, presented in a step-by-step technical manner.


RMI: Compiling and Executing the Server and Client

Introduction

After defining and implementing the remote interface in RMI, the next step is to compile and execute the application. This involves preparing the server, registering the remote object, and running the client to invoke remote methods.

RMI execution follows a multi-step process involving compilation, registry setup, server startup, and client execution.


Components Involved

  • Remote Interface
  • Implementation Class
  • Server Program
  • Client Program
  • RMI Registry

Project Files Example

Calculator.java        (Remote Interface)
CalculatorImpl.java    (Implementation)
Server.java            (Server program)
Client.java            (Client program)

Step 1: Compile All Java Files

Use the javac compiler:

javac *.java

This generates .class files for all components.


Step 2: Generate Stub Classes (Optional in Modern Java)

In older versions of Java:

rmic CalculatorImpl

๐Ÿ“Œ In modern Java (Java 5+), this step is not required because stubs are generated dynamically.


Step 3: Start RMI Registry

The RMI registry acts as a directory service for remote objects.

rmiregistry 1099
  • Default port: 1099
  • Must be started before server

๐Ÿ“Œ Ensure it runs in the same directory as compiled classes.


Step 4: Write and Run Server Program

Server Code Example

import java.rmi.Naming;

public class Server {
    public static void main(String[] args) {
        try {
            CalculatorImpl obj = new CalculatorImpl();
            Naming.rebind("rmi://localhost/CalculatorService", obj);
            System.out.println("Server is ready...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Run Server

java Server

๐Ÿ“Œ Server will:

  • Create remote object
  • Register it with RMI registry

Step 5: Write and Run Client Program

Client Code Example

import java.rmi.Naming;

public class Client {
    public static void main(String[] args) {
        try {
            Calculator obj = (Calculator) Naming.lookup("rmi://localhost/CalculatorService");
            int result = obj.add(10, 20);
            System.out.println("Result = " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Run Client

java Client

Execution Flow

  1. Compile all Java files
  2. Start RMI registry
  3. Run server โ†’ registers object
  4. Run client โ†’ looks up object
  5. Client invokes remote method
  6. Server processes request
  7. Result returned to client

Important Points

  • Server must start before client
  • Registry must be running
  • Same interface must be available on both client and server
  • Network configuration must be correct

Common Errors

  • Connection refused โ†’ Registry not running
  • NotBoundException โ†’ Object not registered
  • ClassNotFoundException โ†’ Missing class files
  • MalformedURLException โ†’ Incorrect URL

Best Practices

  • Always start registry first
  • Use correct classpath
  • Keep interface consistent
  • Handle exceptions properly
  • Use meaningful service names

Conclusion

Compiling and executing an RMI application involves multiple coordinated steps, including compiling classes, starting the RMI registry, running the server to register remote objects, and executing the client to invoke remote methods. Proper execution order and configuration are essential for successful communication in distributed Java applications.