SlideShare a Scribd company logo
Java RMI
Java RMI
i
AbouttheTutorial
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.
Audience
This tutorial has been prepared for beginners to make them understand the basics of
Remote Method Invocation in Java.
Prerequisites
For this tutorial, it is assumed that the readers have a prior knowledge of Java
programming language. In some of the programs of this tutorial, we have used JavaFX for
GUI purpose. So, it is recommended that you go through our JavaFX tutorial before
proceeding further. http://www.tutorialspoint.com/javafx/
Copyright&Disclaimer
© Copyright 2017 by Tutorials Point (I) Pvt. Ltd.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
Java RMI
ii
TableofContents
About the Tutorial .....................................................................................................................................i
Audience....................................................................................................................................................i
Prerequisites..............................................................................................................................................i
Copyright & Disclaimer ..............................................................................................................................i
Table of Contents......................................................................................................................................ii
JAVA RMI ─ INTRODUCTION.................................................................................................1
Architecture of an RMI Application...........................................................................................................1
Working of an RMI Application.................................................................................................................2
Marshalling and Unmarshalling ................................................................................................................2
RMI Registry .............................................................................................................................................3
Goals of RMI .............................................................................................................................................3
JAVA RMI ─ RMI APPLICATION..............................................................................................4
Defining the Remote Interface..................................................................................................................4
Developing the Implementation Class (Remote Object)............................................................................5
Developing the Server Program ................................................................................................................5
Developing the Client Program .................................................................................................................7
Compiling the Application.........................................................................................................................8
Executing the Application .........................................................................................................................9
JAVA RMI ─ GUI APPLICATION............................................................................................11
Server Program.......................................................................................................................................15
Client Program........................................................................................................................................16
Steps to Run the Example .......................................................................................................................17
Java RMI
iii
JAVA RMI ─ DATABASE APPLICATION .................................................................................20
Creating a Student Class..........................................................................................................................20
Server Program.......................................................................................................................................24
Client Program........................................................................................................................................25
Steps to Run the Example .......................................................................................................................26
Java RMI
4
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.
ArchitectureofanRMIApplication
In an RMI application, we write two programs, a server program (resides on the server) and
a client program (resides on the client).
 Inside the server program, a remote object is created and reference of that object is
made available for the client (using the registry).
 The client program requests the remote objects on the server and tries to invoke its
methods.
The following diagram shows the architecture of an RMI application.
JAVA RMI ─ INTRODUCTION
Java RMI
5
Let us now discuss the components of this architecture.
 Transport Layer ─ This layer connects the client and the server. It manages the
existing connection and also sets up new connections.
 Stub ─ A stub is a representation (proxy) of the remote object at client. It resides in
the client system; it acts as a gateway for the client program.
 Skeleton ─ This is the object which resides on the server side. stub communicates
with this skeleton to pass request to the remote object.
 RRL (Remote Reference Layer) ─ It is the layer which manages the references
made by the client to the remote object.
WorkingofanRMIApplication
The following points summarize how an RMI application works:
 When the client makes a call to the remote object, it is received by the stub which
eventually passes this request to the RRL.
 When the client-side RRL receives the request, it invokes a method called invoke() of
the object remoteRef. It passes the request to the RRL on the server side.
 The RRL on the server side passes the request to the Skeleton (proxy on the server)
which finally invokes the required object on the server.
 The result is passed all the way back to the client.
MarshallingandUnmarshalling
Whenever a client invokes a method that accepts parameters on a remote object, the
parameters are bundled into a message before being sent over the network. These
parameters may be of primitive type or objects. In case of primitive type, the parameters are
put together and a header is attached to it. In case the parameters are objects, then they are
serialized. This process is known as marshalling.
At the server side, the packed parameters are unbundled and then the required method is
invoked. This process is known as unmarshalling.
Java RMI
6
RMIRegistry
RMIregistry is a namespace on which all server objects are placed. Each time the server
creates an object, it registers this object with the RMIregistry (using bind() or reBind()
methods). These are registered using a unique name known as bind name.
To invoke a remote object, the client needs a reference of that object. At that time, the client
fetches the object from the registry using its bind name (using lookup() method).
The following illustration explains the entire process:
GoalsofRMI
Following are the goals of RMI:
 To minimize the complexity of the application
 To preserve type safety
Java RMI
7
 Distributed garbage collection
 Minimize the difference between working with local and remote objects
Java RMI
8
To write an RMI Java application, you would have to follow the steps given below:
 Define the remote interface
 Develop the implementation class (remote object)
 Develop the server program
 Develop the client program
 Compile the application
 Execute the application
DefiningtheRemoteInterface
A remote interface provides the description of 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 named
RemoteException may occur; throw it.
Following is an example of a remote interface. Here we have defined an interface with the
name Hello and it has a method called printMsg().
import java.rmi.Remote;
import java.rmi.RemoteException;
// Creating Remote interface for our application
public interface Hello extends Remote {
void printMsg() throws RemoteException;
}
JAVA RMI ─ RMI APPLICATION
Java RMI
9
DevelopingtheImplementationClass(RemoteObject)
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.)
To develop an implementation class –
 Implement the interface created in the previous step.
 Provide implementation to all the abstract methods of the remote interface.
Following is an implementation class. Here, we have created a class named ImplExample
and implemented the interface Hello created in the previous step and provided body for this
method which prints a message.
// Implementing the remote interface
public class ImplExample implements Hello
{
// Implementing the interface method
public void printMsg() {
System.out.println("This is an example RMI program");
}
}
DevelopingtheServerProgram
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.
To develop a server program –
 Create a class that extends the implementation class implemented in the previous
step. (or implement the remote interface)
 Create a remote object by instantiating the implementation class as shown below.
 Export the remote object using the method exportObject() of the class named
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
named Registry. To this method, pass a string representing the bind name and the
object exported, as parameters.
Java RMI
10
Following is an example of an RMI server program.
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 {
// Instantiating the implementation class
ImplExample obj = new ImplExample();
// Exporting the object of implementation class
// (here we are exporting the remote object to the stub)
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Binding the remote object (stub) in the registry
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();
}
}
Java RMI
11
}
DevelopingtheClientProgram
Write a client program in it, fetch the remote object and invoke the required method using
this object.
To develop a client program –
 Create a client class from where you want 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 down cast it.
 The lookup() returns an object of type remote, down cast it to the type Hello.
 Finally invoke the required method using the obtained remote object.
Following is an example of an RMI client program.
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
try {
// Getting the registry
Registry registry = LocateRegistry.getRegistry(null);
// Looking up the registry for the remote object
Hello stub = (Hello) registry.lookup("Hello");
Java RMI
12
// Calling the remote method using the obtained object
stub.printMsg();
// System.out.println("Remote method invoked");
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
CompilingtheApplication
To compile the application –
 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 as
shown below.
Javac *.java
Java RMI
13
ExecutingtheApplication
Step 1: Start the rmi registry using the following command.
start rmiregistry
This will start an rmi registry on a separate window as shown below.
Java RMI
14
Step 2: Run the server class file as shown below.
Java Server
Step 3: Run the client class file as shown below.
java Client
Java RMI
15
Verification: As soon you start the client, you would see the following output in the server.
Java RMI
16
End of ebook preview
If you liked what you saw…
Buy it from our store @ https://store.tutorialspoint.com

More Related Content

What's hot (20)

Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)
Sonali Parab
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
Paul Pajo
 
Java rmi
Java rmiJava rmi
Java rmi
Tanmoy Barman
 
Remote Method Invocation in JAVA
Remote Method Invocation in JAVARemote Method Invocation in JAVA
Remote Method Invocation in JAVA
Jalpesh Vasa
 
Java rmi
Java rmiJava rmi
Java rmi
kamal kotecha
 
Java RMI Detailed Tutorial
Java RMI Detailed TutorialJava RMI Detailed Tutorial
Java RMI Detailed Tutorial
Masud Rahman
 
A Short Java RMI Tutorial
A Short Java RMI TutorialA Short Java RMI Tutorial
A Short Java RMI Tutorial
Guo Albert
 
Introduction To Rmi
Introduction To RmiIntroduction To Rmi
Introduction To Rmi
SwarupKulkarni
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
ashishspace
 
Rmi
RmiRmi
Rmi
leminhvuong
 
Rmi
RmiRmi
Rmi
Vijay Kiran
 
Java RMI(Remote Method Invocation)
Java RMI(Remote Method Invocation)Java RMI(Remote Method Invocation)
Java RMI(Remote Method Invocation)
Nilesh Valva
 
Rmi ppt-2003
Rmi ppt-2003Rmi ppt-2003
Rmi ppt-2003
kalaranjani1990
 
Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)
eLink Business Innovations
 
Distributed Programming using RMI
Distributed Programming using RMIDistributed Programming using RMI
Distributed Programming using RMI
backdoor
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Dew Shishir
 
Java rmi
Java rmiJava rmi
Java rmi
Fazlur Rahman
 
Java remote method invocation
Java remote method invocationJava remote method invocation
Java remote method invocation
Van Dawn
 
Distributes objects and Rmi
Distributes objects and RmiDistributes objects and Rmi
Distributes objects and Rmi
Mayank Jain
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
Ravi Theja
 
Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)
Sonali Parab
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
Paul Pajo
 
Remote Method Invocation in JAVA
Remote Method Invocation in JAVARemote Method Invocation in JAVA
Remote Method Invocation in JAVA
Jalpesh Vasa
 
Java RMI Detailed Tutorial
Java RMI Detailed TutorialJava RMI Detailed Tutorial
Java RMI Detailed Tutorial
Masud Rahman
 
A Short Java RMI Tutorial
A Short Java RMI TutorialA Short Java RMI Tutorial
A Short Java RMI Tutorial
Guo Albert
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
ashishspace
 
Java RMI(Remote Method Invocation)
Java RMI(Remote Method Invocation)Java RMI(Remote Method Invocation)
Java RMI(Remote Method Invocation)
Nilesh Valva
 
Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)
eLink Business Innovations
 
Distributed Programming using RMI
Distributed Programming using RMIDistributed Programming using RMI
Distributed Programming using RMI
backdoor
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Dew Shishir
 
Java remote method invocation
Java remote method invocationJava remote method invocation
Java remote method invocation
Van Dawn
 
Distributes objects and Rmi
Distributes objects and RmiDistributes objects and Rmi
Distributes objects and Rmi
Mayank Jain
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
Ravi Theja
 

Similar to Java rmi tutorial (20)

Java rmi
Java rmiJava rmi
Java rmi
Namomsa Amanuu
 
Remote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programmingRemote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programming
Gera Paulos
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
Sonali Parab
 
Rmi
RmiRmi
Rmi
Mallikarjuna G D
 
ADB Lab Manual.docx
ADB Lab Manual.docxADB Lab Manual.docx
ADB Lab Manual.docx
SaiKumarPrajapathi
 
Java RMI
Java RMIJava RMI
Java RMI
Ankit Desai
 
Report on mini project(Student database handling using RMI)
Report on mini project(Student database handling using RMI)Report on mini project(Student database handling using RMI)
Report on mini project(Student database handling using RMI)
shraddha mane
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
Arun Nair
 
Remote method invocatiom
Remote method invocatiomRemote method invocatiom
Remote method invocatiom
sakthibalabalamuruga
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Veni7
 
Module 3 remote method invocation-2
Module 3   remote method  invocation-2Module 3   remote method  invocation-2
Module 3 remote method invocation-2
Ankit Dubey
 
Distributed Programming (RMI)
Distributed Programming (RMI)Distributed Programming (RMI)
Distributed Programming (RMI)
Ravi Kant Sahu
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
Peter R. Egli
 
Rmi
RmiRmi
Rmi
phanleson
 
11 Distributrd Systems and parallel systems_Chapter 5
11 Distributrd Systems and parallel systems_Chapter 511 Distributrd Systems and parallel systems_Chapter 5
11 Distributrd Systems and parallel systems_Chapter 5
JamilaAllaw
 
Distributed Objects and JAVA
Distributed Objects and JAVADistributed Objects and JAVA
Distributed Objects and JAVA
elliando dias
 
Javarmi 130925082348-phpapp01
Javarmi 130925082348-phpapp01Javarmi 130925082348-phpapp01
Javarmi 130925082348-phpapp01
heenamithadiya
 
Rmi
RmiRmi
Rmi
vantinhkhuc
 
RMI (Remote Method Invocation)
RMI (Remote Method Invocation)RMI (Remote Method Invocation)
RMI (Remote Method Invocation)
Thesis Scientist Private Limited
 
Rmi
RmiRmi
Rmi
Object-Frontier Software Pvt. Ltd
 
Remote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programmingRemote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programming
Gera Paulos
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
Sonali Parab
 
Report on mini project(Student database handling using RMI)
Report on mini project(Student database handling using RMI)Report on mini project(Student database handling using RMI)
Report on mini project(Student database handling using RMI)
shraddha mane
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
Arun Nair
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Veni7
 
Module 3 remote method invocation-2
Module 3   remote method  invocation-2Module 3   remote method  invocation-2
Module 3 remote method invocation-2
Ankit Dubey
 
Distributed Programming (RMI)
Distributed Programming (RMI)Distributed Programming (RMI)
Distributed Programming (RMI)
Ravi Kant Sahu
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
Peter R. Egli
 
11 Distributrd Systems and parallel systems_Chapter 5
11 Distributrd Systems and parallel systems_Chapter 511 Distributrd Systems and parallel systems_Chapter 5
11 Distributrd Systems and parallel systems_Chapter 5
JamilaAllaw
 
Distributed Objects and JAVA
Distributed Objects and JAVADistributed Objects and JAVA
Distributed Objects and JAVA
elliando dias
 
Javarmi 130925082348-phpapp01
Javarmi 130925082348-phpapp01Javarmi 130925082348-phpapp01
Javarmi 130925082348-phpapp01
heenamithadiya
 
Ad

More from HarikaReddy115 (20)

Dbms tutorial
Dbms tutorialDbms tutorial
Dbms tutorial
HarikaReddy115
 
Data structures algorithms_tutorial
Data structures algorithms_tutorialData structures algorithms_tutorial
Data structures algorithms_tutorial
HarikaReddy115
 
Wireless communication tutorial
Wireless communication tutorialWireless communication tutorial
Wireless communication tutorial
HarikaReddy115
 
Cryptography tutorial
Cryptography tutorialCryptography tutorial
Cryptography tutorial
HarikaReddy115
 
Cosmology tutorial
Cosmology tutorialCosmology tutorial
Cosmology tutorial
HarikaReddy115
 
Control systems tutorial
Control systems tutorialControl systems tutorial
Control systems tutorial
HarikaReddy115
 
Computer logical organization_tutorial
Computer logical organization_tutorialComputer logical organization_tutorial
Computer logical organization_tutorial
HarikaReddy115
 
Computer fundamentals tutorial
Computer fundamentals tutorialComputer fundamentals tutorial
Computer fundamentals tutorial
HarikaReddy115
 
Compiler design tutorial
Compiler design tutorialCompiler design tutorial
Compiler design tutorial
HarikaReddy115
 
Communication technologies tutorial
Communication technologies tutorialCommunication technologies tutorial
Communication technologies tutorial
HarikaReddy115
 
Biometrics tutorial
Biometrics tutorialBiometrics tutorial
Biometrics tutorial
HarikaReddy115
 
Behavior driven development_tutorial
Behavior driven development_tutorialBehavior driven development_tutorial
Behavior driven development_tutorial
HarikaReddy115
 
Basics of computers_tutorial
Basics of computers_tutorialBasics of computers_tutorial
Basics of computers_tutorial
HarikaReddy115
 
Basics of computer_science_tutorial
Basics of computer_science_tutorialBasics of computer_science_tutorial
Basics of computer_science_tutorial
HarikaReddy115
 
Basic electronics tutorial
Basic electronics tutorialBasic electronics tutorial
Basic electronics tutorial
HarikaReddy115
 
Auditing tutorial
Auditing tutorialAuditing tutorial
Auditing tutorial
HarikaReddy115
 
Artificial neural network_tutorial
Artificial neural network_tutorialArtificial neural network_tutorial
Artificial neural network_tutorial
HarikaReddy115
 
Artificial intelligence tutorial
Artificial intelligence tutorialArtificial intelligence tutorial
Artificial intelligence tutorial
HarikaReddy115
 
Antenna theory tutorial
Antenna theory tutorialAntenna theory tutorial
Antenna theory tutorial
HarikaReddy115
 
Analog communication tutorial
Analog communication tutorialAnalog communication tutorial
Analog communication tutorial
HarikaReddy115
 
Data structures algorithms_tutorial
Data structures algorithms_tutorialData structures algorithms_tutorial
Data structures algorithms_tutorial
HarikaReddy115
 
Wireless communication tutorial
Wireless communication tutorialWireless communication tutorial
Wireless communication tutorial
HarikaReddy115
 
Control systems tutorial
Control systems tutorialControl systems tutorial
Control systems tutorial
HarikaReddy115
 
Computer logical organization_tutorial
Computer logical organization_tutorialComputer logical organization_tutorial
Computer logical organization_tutorial
HarikaReddy115
 
Computer fundamentals tutorial
Computer fundamentals tutorialComputer fundamentals tutorial
Computer fundamentals tutorial
HarikaReddy115
 
Compiler design tutorial
Compiler design tutorialCompiler design tutorial
Compiler design tutorial
HarikaReddy115
 
Communication technologies tutorial
Communication technologies tutorialCommunication technologies tutorial
Communication technologies tutorial
HarikaReddy115
 
Behavior driven development_tutorial
Behavior driven development_tutorialBehavior driven development_tutorial
Behavior driven development_tutorial
HarikaReddy115
 
Basics of computers_tutorial
Basics of computers_tutorialBasics of computers_tutorial
Basics of computers_tutorial
HarikaReddy115
 
Basics of computer_science_tutorial
Basics of computer_science_tutorialBasics of computer_science_tutorial
Basics of computer_science_tutorial
HarikaReddy115
 
Basic electronics tutorial
Basic electronics tutorialBasic electronics tutorial
Basic electronics tutorial
HarikaReddy115
 
Artificial neural network_tutorial
Artificial neural network_tutorialArtificial neural network_tutorial
Artificial neural network_tutorial
HarikaReddy115
 
Artificial intelligence tutorial
Artificial intelligence tutorialArtificial intelligence tutorial
Artificial intelligence tutorial
HarikaReddy115
 
Antenna theory tutorial
Antenna theory tutorialAntenna theory tutorial
Antenna theory tutorial
HarikaReddy115
 
Analog communication tutorial
Analog communication tutorialAnalog communication tutorial
Analog communication tutorial
HarikaReddy115
 
Ad

Recently uploaded (20)

THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATIONTHE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
PROF. PAUL ALLIEU KAMARA
 
PHYSIOLOGY & SPORTS INJURY by Diwakar Sir
PHYSIOLOGY & SPORTS INJURY by Diwakar SirPHYSIOLOGY & SPORTS INJURY by Diwakar Sir
PHYSIOLOGY & SPORTS INJURY by Diwakar Sir
Diwakar Kashyap
 
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly WorkshopsLDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDM & Mia eStudios
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
LDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-inLDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-in
LDM & Mia eStudios
 
Odoo 18 Point of Sale PWA - Odoo Slides
Odoo 18 Point of Sale PWA  - Odoo  SlidesOdoo 18 Point of Sale PWA  - Odoo  Slides
Odoo 18 Point of Sale PWA - Odoo Slides
Celine George
 
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdfপ্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
Pragya - UEM Kolkata Quiz Club
 
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT PatnaSwachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Quiz Club, Indian Institute of Technology, Patna
 
Uterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managmentUterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managment
Ritu480198
 
Types of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo SlidesTypes of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo Slides
Celine George
 
Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..
faizanaltaf231
 
How to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guidesHow to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guides
Celine George
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
Introduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdfIntroduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdf
CME4Life
 
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
 
LDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College VolumeLDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College Volume
LDM & Mia eStudios
 
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based EducatorDiana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda
 
"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx
Arshad Shaikh
 
Order: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptxOrder: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptx
Arshad Shaikh
 
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATIONTHE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
PROF. PAUL ALLIEU KAMARA
 
PHYSIOLOGY & SPORTS INJURY by Diwakar Sir
PHYSIOLOGY & SPORTS INJURY by Diwakar SirPHYSIOLOGY & SPORTS INJURY by Diwakar Sir
PHYSIOLOGY & SPORTS INJURY by Diwakar Sir
Diwakar Kashyap
 
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly WorkshopsLDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDM & Mia eStudios
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
LDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-inLDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-in
LDM & Mia eStudios
 
Odoo 18 Point of Sale PWA - Odoo Slides
Odoo 18 Point of Sale PWA  - Odoo  SlidesOdoo 18 Point of Sale PWA  - Odoo  Slides
Odoo 18 Point of Sale PWA - Odoo Slides
Celine George
 
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdfপ্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
Pragya - UEM Kolkata Quiz Club
 
Uterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managmentUterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managment
Ritu480198
 
Types of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo SlidesTypes of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo Slides
Celine George
 
Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..
faizanaltaf231
 
How to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guidesHow to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guides
Celine George
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
Introduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdfIntroduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdf
CME4Life
 
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
 
LDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College VolumeLDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College Volume
LDM & Mia eStudios
 
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based EducatorDiana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda
 
"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx
Arshad Shaikh
 
Order: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptxOrder: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptx
Arshad Shaikh
 

Java rmi tutorial

  • 2. Java RMI i AbouttheTutorial 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. Audience This tutorial has been prepared for beginners to make them understand the basics of Remote Method Invocation in Java. Prerequisites For this tutorial, it is assumed that the readers have a prior knowledge of Java programming language. In some of the programs of this tutorial, we have used JavaFX for GUI purpose. So, it is recommended that you go through our JavaFX tutorial before proceeding further. http://www.tutorialspoint.com/javafx/ Copyright&Disclaimer © Copyright 2017 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected]
  • 3. Java RMI ii TableofContents About the Tutorial .....................................................................................................................................i Audience....................................................................................................................................................i Prerequisites..............................................................................................................................................i Copyright & Disclaimer ..............................................................................................................................i Table of Contents......................................................................................................................................ii JAVA RMI ─ INTRODUCTION.................................................................................................1 Architecture of an RMI Application...........................................................................................................1 Working of an RMI Application.................................................................................................................2 Marshalling and Unmarshalling ................................................................................................................2 RMI Registry .............................................................................................................................................3 Goals of RMI .............................................................................................................................................3 JAVA RMI ─ RMI APPLICATION..............................................................................................4 Defining the Remote Interface..................................................................................................................4 Developing the Implementation Class (Remote Object)............................................................................5 Developing the Server Program ................................................................................................................5 Developing the Client Program .................................................................................................................7 Compiling the Application.........................................................................................................................8 Executing the Application .........................................................................................................................9 JAVA RMI ─ GUI APPLICATION............................................................................................11 Server Program.......................................................................................................................................15 Client Program........................................................................................................................................16 Steps to Run the Example .......................................................................................................................17
  • 4. Java RMI iii JAVA RMI ─ DATABASE APPLICATION .................................................................................20 Creating a Student Class..........................................................................................................................20 Server Program.......................................................................................................................................24 Client Program........................................................................................................................................25 Steps to Run the Example .......................................................................................................................26
  • 5. Java RMI 4 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. ArchitectureofanRMIApplication In an RMI application, we write two programs, a server program (resides on the server) and a client program (resides on the client).  Inside the server program, a remote object is created and reference of that object is made available for the client (using the registry).  The client program requests the remote objects on the server and tries to invoke its methods. The following diagram shows the architecture of an RMI application. JAVA RMI ─ INTRODUCTION
  • 6. Java RMI 5 Let us now discuss the components of this architecture.  Transport Layer ─ This layer connects the client and the server. It manages the existing connection and also sets up new connections.  Stub ─ A stub is a representation (proxy) of the remote object at client. It resides in the client system; it acts as a gateway for the client program.  Skeleton ─ This is the object which resides on the server side. stub communicates with this skeleton to pass request to the remote object.  RRL (Remote Reference Layer) ─ It is the layer which manages the references made by the client to the remote object. WorkingofanRMIApplication The following points summarize how an RMI application works:  When the client makes a call to the remote object, it is received by the stub which eventually passes this request to the RRL.  When the client-side RRL receives the request, it invokes a method called invoke() of the object remoteRef. It passes the request to the RRL on the server side.  The RRL on the server side passes the request to the Skeleton (proxy on the server) which finally invokes the required object on the server.  The result is passed all the way back to the client. MarshallingandUnmarshalling Whenever a client invokes a method that accepts parameters on a remote object, the parameters are bundled into a message before being sent over the network. These parameters may be of primitive type or objects. In case of primitive type, the parameters are put together and a header is attached to it. In case the parameters are objects, then they are serialized. This process is known as marshalling. At the server side, the packed parameters are unbundled and then the required method is invoked. This process is known as unmarshalling.
  • 7. Java RMI 6 RMIRegistry RMIregistry is a namespace on which all server objects are placed. Each time the server creates an object, it registers this object with the RMIregistry (using bind() or reBind() methods). These are registered using a unique name known as bind name. To invoke a remote object, the client needs a reference of that object. At that time, the client fetches the object from the registry using its bind name (using lookup() method). The following illustration explains the entire process: GoalsofRMI Following are the goals of RMI:  To minimize the complexity of the application  To preserve type safety
  • 8. Java RMI 7  Distributed garbage collection  Minimize the difference between working with local and remote objects
  • 9. Java RMI 8 To write an RMI Java application, you would have to follow the steps given below:  Define the remote interface  Develop the implementation class (remote object)  Develop the server program  Develop the client program  Compile the application  Execute the application DefiningtheRemoteInterface A remote interface provides the description of 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 named RemoteException may occur; throw it. Following is an example of a remote interface. Here we have defined an interface with the name Hello and it has a method called printMsg(). import java.rmi.Remote; import java.rmi.RemoteException; // Creating Remote interface for our application public interface Hello extends Remote { void printMsg() throws RemoteException; } JAVA RMI ─ RMI APPLICATION
  • 10. Java RMI 9 DevelopingtheImplementationClass(RemoteObject) 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.) To develop an implementation class –  Implement the interface created in the previous step.  Provide implementation to all the abstract methods of the remote interface. Following is an implementation class. Here, we have created a class named ImplExample and implemented the interface Hello created in the previous step and provided body for this method which prints a message. // Implementing the remote interface public class ImplExample implements Hello { // Implementing the interface method public void printMsg() { System.out.println("This is an example RMI program"); } } DevelopingtheServerProgram 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. To develop a server program –  Create a class that extends the implementation class implemented in the previous step. (or implement the remote interface)  Create a remote object by instantiating the implementation class as shown below.  Export the remote object using the method exportObject() of the class named 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 named Registry. To this method, pass a string representing the bind name and the object exported, as parameters.
  • 11. Java RMI 10 Following is an example of an RMI server program. 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 { // Instantiating the implementation class ImplExample obj = new ImplExample(); // Exporting the object of implementation class // (here we are exporting the remote object to the stub) Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0); // Binding the remote object (stub) in the registry 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(); } }
  • 12. Java RMI 11 } DevelopingtheClientProgram Write a client program in it, fetch the remote object and invoke the required method using this object. To develop a client program –  Create a client class from where you want 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 down cast it.  The lookup() returns an object of type remote, down cast it to the type Hello.  Finally invoke the required method using the obtained remote object. Following is an example of an RMI client program. import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class Client { private Client() {} public static void main(String[] args) { try { // Getting the registry Registry registry = LocateRegistry.getRegistry(null); // Looking up the registry for the remote object Hello stub = (Hello) registry.lookup("Hello");
  • 13. Java RMI 12 // Calling the remote method using the obtained object stub.printMsg(); // System.out.println("Remote method invoked"); } catch (Exception e) { System.err.println("Client exception: " + e.toString()); e.printStackTrace(); } } } CompilingtheApplication To compile the application –  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 as shown below. Javac *.java
  • 14. Java RMI 13 ExecutingtheApplication Step 1: Start the rmi registry using the following command. start rmiregistry This will start an rmi registry on a separate window as shown below.
  • 15. Java RMI 14 Step 2: Run the server class file as shown below. Java Server Step 3: Run the client class file as shown below. java Client
  • 16. Java RMI 15 Verification: As soon you start the client, you would see the following output in the server.
  • 17. Java RMI 16 End of ebook preview If you liked what you saw… Buy it from our store @ https://store.tutorialspoint.com