
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Send Data Over Remote Method in Java
RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing/running in one system (JVM) to access/invoke an object running on another JVM.
RMI is used to build distributed applications; it provides remote communication between Java programs. It is provided in the package java.rmi. To create an RMI Java application, you need to follow the steps given below -
Step 1: Define the Remote Interface
A remote interface provides the description of all the methods of a particular remote object. The client communicates with this remote interface. Therefore, you need to create an interface that extends the predefined interface, java.rmi.Remote -
Example
import java.rmi.Remote; import java.rmi.RemoteException; public interface Hello extends Remote { void printMsg() throws RemoteException; }
Step 2: Develop the Implementation Class
We need to implement the remote interface created in the earlier step. (We can write an implementation class separately, or the server program itself can implement this interface.) Therefore, provide implementation for all the abstract methods of the remote interface.
Example
public class ImplExample implements Hello { public void printMsg() { System.out.println("This is an example RMI program"); } }
Step 3: Develop the Server Program
An RMI server program should implement the remote interface or extend the implementation class. Here, we should create a remote object and bind it to the RMIregistry. Therefore, develop a server program as shown below -
Example
import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class Server extends ImplExample { public Server() {} public static void main(String args[]) { try { ImplExample obj = new ImplExample(); Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0); Registry registry = LocateRegistry.getRegistry(); registry.bind("Hello", stub); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception: " + e.toString()); e.printStackTrace(); } } }
Step 4: Develop the Client Program
Write a client program in it, fetch the remote object, and invoke the required method using this object.
Example
import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class Client { private Client() {} public static void main(String[] args) { try { Registry registry = LocateRegistry.getRegistry(null); Hello stub = (Hello) registry.lookup("Hello"); stub.printMsg(); } catch (Exception e) { System.err.println("Client exception: " + e.toString()); e.printStackTrace(); } } }
Step 5: Compile the Application
To compile the application -
-
Compile the Remote interface.
-
Compile the implementation class.
-
Compile the server program.
-
Compile the client program.
Step 6: Execute the Application
Finally, to execute the application -
-
Start the rmi registry using the command Start rmiregistry. This will start an rmi registry on a separate window.
-
Run the server class file as shown below -
-
Run the client class file as shown below -
-
Verification: As soon as you start the client, you will see the following output on the server.
Exceptions
-
RemoteException: It must be declared in every remote method because remote communication might fail. Always use try-catch blocks for RMI invocations for better error handling.
-
NotBoundException: If the invoked or called name is not currently bound in the registry, NotBoundException will be thrown.
-
AccessException: If the invocation is not permitted due to access control, AccessException will be thrown.