To create Java RMI Application following steps are needed
- Step 1 - Define the Remote Interface
- Step 2 - Develop the Implementation Class or Remote Object
- Step 3 - Develop the Server Program
- Step 4 - Develop the Client Program
- Step 5 - Compile the Application
- Step 6 - Execute the Application
Step 1 - Define the Remote Interface
- A remote interface provides describes all the methods of a particular remote object.
- The client communicates with this remote interface.
- To create a remote interface −
- Create an interface that extends the predefined interface Remote which belongs to the package.
- Declare all the business methods that can be invoked by the client in this interface.
- Since there is a chance of network issues during remote calls, an exception RemoteException may occur; throw it.
Step 2 - Develop the Implementation Class or Remote Object
- Implementation Class or Remote Object can be written separately or can be written directly with the server program implementation.
- To develop an implementation class −
- Implement the interface created in the previous step.
- Provide implementation to all the abstract methods of the remote interface.
Step 3 - Develop the Server Program
- An RMI server program should implement the remote interface or extend the implementation class.
To develop a server program −
- Create a client class from where you want to invoke the remote object.
- Create a remote object by instantiating the implementation class as shown below.
- Export the remote object using the method exportObject() of the class UnicastRemoteObject which belongs to the package java.rmi.server.
- Get the RMI registry using the getRegistry() method of the LocateRegistry class which belongs to the package java.rmi.registry.
- Bind the remote object created to the registry using the bind() method of the class Registry. To this method, pass a string representing the bind name and the object exported, as parameters.
Step 4 - Develop the Client Program
- Write a client program, fetch the remote object and invoke the required method using the remote object.
To develop a client program −
- Create a client class from where your are intended to invoke the remote object.
- Get the RMI registry using the getRegistry() method of the LocateRegistry class which belongs to the package java.rmi.registry.
- Fetch the object from the registry using the method lookup() of the class Registry which belongs to the package java.rmi.registry.
- To this method, you need to pass a string value representing the bind name as a parameter. This will return you the remote object.
- The lookup() returns an object of type remote.
- Finally, invoke the required method using the obtained remote object.
Step 5 - Compile the Application
Compile the application in the following manner
- Compile the Remote Interface.
- Compile the Implementation Class.
- Compile the Server Program.
- Compile the Client Program. Or,
- Open the folder where you have stored all the programs and compile all the Java files.
Step 6 - Execute the Application
- Step 1 - Start the RMI registry using the command.
start rmiregistry
This will start an RMI registry in a separate window.
- Step 2 − Run the server class file
java Server
- Step 3 − Run the client class file
java Client