Add to Google Reader or Homepage

Java RMI Tutorial

How to perform Remote Method Invocation in java

What is meant  by RMI?

Remote Method Invocation refers to invoking a method on an object which resides in a remote machine.For example, let's take the client/server model. In which the server is  usually a remote machine and the client invokes a method of an object which resides in the server to accomplish the required task.

Let us see what are the components needed  by us to perform  the RMI,

Server - A remote machine which serves us the requested operation.

Client - A machine that requests the server to perform some tasks on its behalf.

RMI Registry

In general, if you want to invoke a method of a class what do you actually need? an object of that class. If the class is available in your machine, then there is no problem for you to create an object of that class and invoke the method.

But in RMI the class for which you would like to create an object is not in your machine it is available in the remote machine i.e, the server. So you would need some means to get the object of that class(in the server). What to do? RMI registry has been created for this purpose.

Consider RMI registry as a book where in the objects needed by the clients are registered by the server with a name (in our case it is a RMI url) ,So if any clients needs  an object, it will request the RMI registry(book) with a name, then the RMI registry provides the object corresponding to that name to the client. Now client can access the methods.

 Stub - I would consider a stub as a cached copy of the remote object in the client machine. What is a remote object? An object of a class that implements the Remote Interface is called remote object.  

what do you meant by Remote Interface? An interface that extends the Remote Interface of the java.rmi package

Why should i need to extend the remote interface?

If you want to make a method to be remotely accessible then you should include this method in the interface that extends the Remote Interface.

For example,

import java.rmi.*;
public interface NameCollections extends Remote //Remote interface
public String getName(int index)throws RemoteException;//Remotely 
                                                                                                                                   accessible method

 A class that implements the Remote interface is like as follows:

import java.rmi.*;
import java.util.*;
import java.rmi.server.*;

class NameStorage extends UnicastRemoteObject implements NameCollections //A
                                                                                                    class that implements the remote interface
private Map name;
public NameStorage()throws RemoteException

name=new HashMap();
public String getName(int index)throws RemoteException
String str=name.get(new Integer(index));
return str;

 NameStorage name=new NameStorage(); //Here name is the remote object

Skeleton - It is exactly similar to the stub but it resides in the server machine, After java 1.2 the skeleton has been omitted. So let's leave this component.


import java.rmi.*;
import javax.naming.*;
class NameServer 
public static void main(String args[])throws RemoteException,NamingException
Context ictxt=new InitialContext();
NameStorage store=new NameStorage();
ictxt.bind("rmi:Storage",store);//Binding the remote object(store) with a 
System.out.println("Clients can invoke the methods....");


import java.rmi.*;
import java.util.*;
import javax.naming.*;
class NameClient
public static void main(String args[])throws NamingException,RemoteException
Context itxt=new InitialContext();
//Searching for the remote object in the RMI registry  
NameCollections coll=(NameCollections)itxt.lookup("rmi://localhost/Storage");
String n1=coll.getName(2);// Invoking the remote method
System.out.println("Name Obtained from the server:"+n1);

Though it appears as invoking the remote method, it doesn't actually invoke the remote method, then what? It actually invokes the method on the stub object(which is available in the client machine).

This stub object would in turn  call the remote method which is available in the server and forwards the return value from the server to  the  client.

Since it is a lengthy post, i have dealt with the execution of the RMI program in another
 post How to execute a RMI program .  


sarabjeet said...

i really like that you are giving information on core and advance java concepts. Being enrolled at i found your information very helpful indeed.thanks for it.

Sean K said...

It seems that RMI is not actively supported by Oracle-JavaSoft. The user-group email is not responding, and the archive is not accessible.

I have a problem where the RMI Naming.lookup does not work when the url passed to lookup is a IPv6 (rmi://[IPv6 address]:4242/Controller

Ganesh Bhuddhan said...

What is the error message? that you encountered,when you try to look up in the url (i.e,IPv6 address).. and have you tried it with IPv4 address?

Hima P said...
This comment has been removed by a blog administrator.
vijay kumar said...

NICE BLOG!!! Thanks for sharing useful information about FNT Softwre Solutions and being one of best Software Training institute in Bangalore we agree that this blog is very useful for the students who are searching for best software courses, I would really like to come back again right here for like wise good articles or blog posts. Thanks for sharing...Java training company bangalore.

Naviya Nair said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai

Vivek Reddy said...

I would say while reading your article, i felt very proud. Because the information you written very useful, please keep posting this type of articles. If you guys looking for a Advanced Java Training in Bangalore. please click below link.

Best Institute For Java in Marathahalli Bangalore

Infocampus Logics Pvt Ltd said...

I really like all the points you made.
Web Designing Training In Bangalore

Web Design Courses In Bangalore

Java Training In Bangalore

iOS Training In Bangalore

vivek gajjala said...

I quite impressed with your java concepts and questionnaire, we would happy to go through these concepts and we also train on IT technologies such as Java Training in Marathahalli Bangalore. Kindly visit our site for free demo.

Dipanwita said...

Loved this blog. Very useful and interesting. java training in chennai

Unknown said...

very informative message and it is very useful. best java training in chennai

Smita Patel said...

Come & Learn Java from best Java training institute in Ahmedabad and make a successful career with java training institute from Ahmedabad.
Java training institute in Ahmedabad

sasha asha said...

Nice post.. keep sharing.. java training in chennai | java training center in chennai | java training institute in chennai

Ananya Krishnan said...

Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work

DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.

Good to learn about DevOps at this time.

devops training in chennai | devops training in chennai with placement | devops training in chennai omr | devops training in velachery | devops training in chennai tambaram | devops institutes in chennai | devops certification in chennai | trending technologies list 2018

Praylin S said...

Great work! Really nice post.. Keep us updated.
Placement Training in Chennai
Placement Training institutes
Placement Training Centres in Chennai
Unix Training in Chennai
Tally Institute in Chennai
Advanced Excel Training in Chennai

SeoIndhu said...

Big thumbs up for making such a wonderful blog with great content.
wifi camera in chennai |

Fire extinguisher in chennai |

Fire alarm systems in chennai

SeoIndhu said...

Great explanation, your article inspired me the most. Thanks for sharing.....
Automatic water level controller in OMR |

Water level indicator in coimbatore |

Automatic water level controller in coimbatore

SeoIndhu said...

Thanks for your post. This is excellent information. The list of your blogs is very helpful for those who want to learn, It is amazing!!! You have been helping many application.....
Garments Rack in Chennai |

Four sided racks in Chennai |

Supermarket rack in Chennai

Sivag said...

Thanks for sharing a nice Blog. Very helpful Information. I got Knowledge by reading your blog. Keep updating more....
Spoken english training in chennai |

Tally training in chennai |

Tally training in vadapalani

Evelin Harriet said...

The blog you have shared is really work for me.Thanks for Sharing..
SAP B1 in Chennai | SAP S/4 Hana in Chennai | SAP R3 in Chennai | ERP Providers in Chennai | HRMS Software Chennai

Service Center Apple said...

Kursus Teknisi Service HP
Distributor Kuota
PT Lampung Service
Service Center HP Indonesian
Service Center iPhone Indonesian
PT Lampung Service

Service Center Apple said...

Pelatihan Kursus Teknisi Service HP Bandar Lampung
Kursus Teknisi Service HP Bandar Lampung
Kursus Teknisi Service HP Bandar Lampung
Bimbel Lampung
Kursus Teknisi Service HP Bandar Lampung
Kursus Teknisi Service HP Bandar Lampung

Service Center Apple said...

Service Center Samsung Bandar Lampung
Service Center Panasonic Bandar Lampung
Kursus Teknisi Service HP
Service Center Huawei Bandar Lampung
Service Center iPhone Bandar Lampung

Service Center Apple said...

Service Center iPhone Bandar Lampung
Service HP Pringsewu LampungYoutuber Lampung
Service HP Pringsewu LampungService Center Acer Indonesian
Lampung ServiceService Center Apple

Post a Comment

java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top