Project 3
Project 3
The first step in creating an RMI application is to define a remote interface. This interface specifies the
methods that can be invoked remotely.
Key Points:
Methods Throw RemoteException: All methods declared in the interface must throw RemoteException,
which is a subclass of IOException.
java
import java.rmi.Remote;
import java.rmi.RemoteException;
Next, you need to implement the remote interface. This implementation class will provide the actual
logic for the methods declared in the interface.
Key Points:
java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
super();
@Override
Historically, RMI required generating stub and skeleton files using the rmic tool. However, since Java 5,
the stub generation is automatic at runtime, so you don't need to manually run rmic unless you're using
an older version of Java.
Note:
Automatic Stub Generation: In modern Java versions, stubs are generated automatically when the server
starts.
4. Start the RMI Registry
The RMI registry is a service that allows clients to look up remote objects by name. You need to start the
registry before running the server.
Command:
bash
rmiregistry 1099
Port Number: The default port for RMI is 1099. You can specify a different port if needed.
The server application creates an instance of the remote object and binds it to a name in the RMI
registry.
Key Points:
Handle Exceptions: Handle any exceptions that might occur during binding.
java
import java.rmi.Naming;
try {
System.out.println("Server is ready.");
} catch (Exception e) {
e.printStackTrace();
The client application looks up the remote object by name and invokes its methods.
Key Points:
Cast to Remote Interface: Cast the retrieved object to the remote interface type.
java
import java.rmi.Naming;
try {
} catch (Exception e) {
e.printStackTrace();
Running Instructions
This setup allows the client to invoke methods on the server remotely using RMI.