
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to send data over remote method in java?
RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing 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 write an RMI Java application, you would have to follow the steps given below −
Step1 − 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 (Remote Object)
We need to implement the remote interface created in the earlier step. (We can write an implementation class separately or we can directly make the server program implement this interface.) Therefore, provide implementation to 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"); } }
Step3 − 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(); } } }
Step4 − 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(); } } }
Step5 − Compile the Application
To compile the application −
Compile the Remote interface.
Compile the implementation class.
Compile the server program.
Compile the client program.
Step6 − Execute the Application
Start the rmi registry using the following 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 you start the client, you would see the following output in the server.
- Related Articles
- How to use POST method to send data in jQuery Ajax?
- How to use GET method to send data in jQuery Ajax?
- How to send data to previous activity in Android?
- How to send data through wifi in android programmatically?
- How to send data back to the main Activity in android?
- How to send data from one Fragment to another Fragment in Android?
- How to send data from one activity to another in Android using intent?
- How to send data from one activity to another in Android using bundle?
- How to send data from one activity to another in Android without intent?
- How to send data back to the Main Activity in Android using Kotlin?
- How to send row data when clicking the button using javascript?
- How to Send and Receive JSON Data to and from the Server
- Send multiple data with ajax in PHP
- send(), sendStatus() and json() method in Node.js
- How to iterate over a list in Java?
