Home arrow Java arrow Page 3 - An Introduction To RMI With Java

An Introduction To RMI With Java

Remote Method Invocation (or RMI for short) allows us to execute methods on remote servers. In this article Nevile introduces us to RMI with a simple example and fundamentals in Java.

Author Info:
By: Neville Mehta
Rating: 4 stars4 stars4 stars4 stars4 stars / 142
June 24, 2002
  1. · An Introduction To RMI With Java
  2. · The Concept
  3. · What Do We Have To Code?
  4. · The Coding (Contd.)
  5. · Conclusion

print this article

An Introduction To RMI With Java - What Do We Have To Code?
(Page 3 of 5 )

Now, before we jump into the code, let me tell you exactly what we have to code:
  • The Remote Object: This interface (yes our remote object is actually an interface) will have only method declarations. Hopefully you know that interfaces donít always have to have method bodies, just declarations. The Remote Object will have a declaration for each method that you want to export. This remote object would implement the Remote interface, which is present in the Java.rmi package.
  • The Remote Object Implementation: This is a class that implements the Remote Object. If you implement the Remote Object, it's common sense that you would override all of the methods in that object, so the remote object implementation class would actually have all of the method bodies of the methods that we want to export. This method will be extended from the UnicastRemoteObject class.
  • The Remote Server: This is a class that will act like a server to the client wanting to access remote methods. Here's the place where you will bind any string with the object you want to export. The binding process will be taken care of in this class.
  • The Remote Client: This is a class that will help you access the remote method. This is the end user, the client. You will call the remote method from this class. You will use methods to search and invoke that remote method.
Ok enough of the theory for now. Let's jump into the fun part: the coding...

The Coding
We will start by coding the remote object. Save this code as AddServer.Java in your favorite text editor:

import Java.rmi.*;

public interface AddServer extends Remote {

public int AddNumbers(int firstnumber,int secondnumber) throws RemoteException;


Let's have a look at this code. First of all we import the rmi package to use its contents. We then create an interface that extends the remote interface present in the Java.rmi package. All remote objects must extend the remote interface. We call this remote object AddServer. We have a method called AddNumbers in this remote object, which could be called by a client. We must remember that all remote methods need to throw the RemoteException, which is called whenever some error occurs.

We will now start coding the remote object implementation. This is the class that would implement the remote object and contain all of the method bodies. Save this code as AddServerImpl.Java in your favorite text editor:

import Java.rmi.*;

public class AddServerImpl extends UnicastRemoteObject implements AddServer {
public AddServerImpl() {
public int AddNumbers(int firstnumber,int secondnumber) throws RemoteException {
return firstnumber + secondnumber;

First of all we import the rmi package to use its contents. We then create a class that extends the UnicastRemoteObject and implements out remote object that we have created. Next, we create a default constructor for our class, which with the help of the super keyword calls the constructor of the base class UnicastRemoteObject. We also see the body for the AddNumbers method, which throws a RemoteException. This way we are actually overriding the method in the remote object that we have created. The method body should be quite self-explainatory. We are receiving two integer parameters, adding them both and then returning their sum.

At this point we have two Java files: the remote object and the remote object implementation. We will now compile both of these file using the Javac command, like this:

Compile Remote Object:
C:\jdk\bin\Javac workingdir\AddServer.Java

Compile Remote Object Implementation:
C:\jdk\bin\Javac workingdir\AddServerImpl.Java

We end up with two Java files and two class files. We are now going to create our stub and skeleton. For creating the stub and skeleton files we have to use the rmic compiler on the remote object implementation file.

Rmic Compile Remote Object Implementation:
C:\jdk\bin\rmic workingdir\AddServerImpl.Java

After following the above step you will see that two newer class files have been created. They are AddServerImpl_Stub.class (the stub which will reside on the client side) and AddServerImpl_Skel.class (the skeleton which will reside on the server side).
blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials