Advertisement
Home > Coding Tips on Java > RMI Example > RMI CodePlease log in to add or reply to any matter<- requires login or
RMI Example

Home > Coding Tips on Java > RMI Example > RMI Code
Struts Tutorials:
Struts2 Tag CheckBoxList
, Checkbox, Iterator, IF
Struts2 Tag Library Example Struts2 Tiles Example Struts2 Tiles I18N Example Struts2 Questions Struts Tiles I18N Example Struts Eclipse MVC Struts2 Tags Struts2 Example and Tutorial Struts MVC Struts2 Validation
Hibernate Tutorials: Hibernate Case Study Class Hierarchy Persist Example Using Hibernate Interceptor Hibernate Questions with Answer Hibernate Many-to-Many Mapping Example Hibernate one-to-many Mapping Example Hibernate and ORM tools Spring Hibernate Example Hibernate SessionFactory Example Hibernate Mapping Class Hierarchy Hibernate Questions Hibernate SessionFactory Questions Spring Hibernate Example: Spring Hibernate Case Study

Written By : Amit
Title :
Composite keys in Hashtable
Description : key and value pair
More...


Written By : Amit
Title :
Customizing JTree using Swing
Description : AWT using Java Technology
More...


Written By : Amit
Title :
Event handling and Java Technology
Description : Event handling and Java Technology
More...


Written By : admin
Title :
Using Final Keyword
Description : Example on using Final Keyword in Java code
More...


Written By : Amit
Title :
In memory AWT Image Creation
Description : Java Code
More...


Written By : Amit
Title :
Java Integral Types
Description : Java Types with Bit size
More...

Java RMI, RMI Example, RMI Code, Java RMI Example, Java Platform
Author : Amit
Date (Year/Month/Date): 2009-07-10 Java RMI Example with code explained

Tags: RMI example, RMI Code, Java RMI example

If you have started learning Java technology recently, then this example
could be very basic and preliminary help in understanding RMI using an
example code. If you already know RMI, then this example could serve you
with a refresh of this concept with very basic example code.

Advertisement
My Software environment for this example: 1. Eclipse 3.2 2. JDK 1.5 Idea here is to show way to call a remote method from a different JVM from a command line based application. So we have two command line based program, one program locates an already running RMIRegistry with a port number 1099, and binds the remote object (an object from a class that extends java.rmi.server.UnicastRemoteObject and implements an interface that extends java.rmi.Remote). And the other program locates an already running RMIRegistry and lookup the already bound remote object. So we have to define an interface that extends java.rmi.Remote and a class that extends UnicastRemoteObject and implements this interface. ExampleRemote.java
//This code is provided "AS IS"
package example;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ExampleRemote extends Remote {
  public String test() throws RemoteException;
}
ExampleRemoteObject.java
//This code is provided "AS IS"
package example;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class ExampleRemoteObject extends UnicastRemoteObject
          implements ExampleRemote {
    public ExampleRemoteObject() throws RemoteException {
      super();
    }
    public String test() throws RemoteException {
      System.out.println("inside test method in ExampleRemoteObject");
      return "from test method";
    }
}
Those two commandline Java programs as follows: TestClient.java
//This code is provided "AS IS"
package example;

import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;

public class TestClient {

  public TestClient() {
    try {
      ExampleRemoteObject remoteObject = new ExampleRemoteObject();
      Registry registry = LocateRegistry.getRegistry("localhost", 1099);
      registry.bind("exampleremote", remoteObject );
    } catch (RemoteException e) {
      e.printStackTrace();
    } catch (AlreadyBoundException e) {
      e.printStackTrace();
    }
  }
  
  public static void main(String[] args) {
    new TestClient();
  }
}
The other program that should be called from another command prompt, this way we can simulate environment with two different JVM and ClassLoader. TestRMIClient.java
//This code is provided "AS IS"
package example;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class TestRMIClient {

  public TestRMIClient() {
    try {
      Registry registry = LocateRegistry.getRegistry("localhost",1099);
      ExampleRemote exampleR = (ExampleRemote) registry.lookup("exampleremote");
      System.out.println(exampleR.test());
    } catch (RemoteException e) {
      e.printStackTrace();
    } catch (NotBoundException e) {
      e.printStackTrace();
    }
		
  }

  public static void main(String[] args) {
    new TestRMIClient();
  }
}
One question may you be thinking is what is this RMI registry that is located in the client programs with a HOST address as localhost and port as 1099? This is nothing but the rmiregistry.exe program found under %JAVA_HOME%\bin folder. One thing to remember is that, before running this rmiregistry.exe program, classpath environment should be having the folder to the example.ExampleRemote class, or else you might get exception such as Caused by: java.lang.ClassNotFoundException: example.ExampleRemote If everything setup properly, then one may run TestClient to have this program stays running till the other client program, that is TestRMIClient is run, in another command prompt, to receive system out as follows: java TestClient inside test method in ExampleRemoteObject java TestRMIClient from test method This shows that TestRMIClient is able to invoke test method from the other TestClient running program and able to receive "from test method" returned value from the test method.
Advertisement
In my another example on RMI, I shall explore way to use Spring Remoting feature with RMI. Please keeep reading my examples and comment on this to improve my examples, and please let me know if this really helped you with your understanding of the topic on hand.
Author of this article/writeup has expressed his/her willingness
to help or guide users with any technical difficulties he/she faces while working with the example code environment setting up, running and resolving any such exception raised during compile or at runtime. You may ask for any technical doubt or seek technical help related to this article by using following form to reach for technical help from the Author for FREE. This article's Author shall be reading your request and responding within reasonable time (no resolution timeframe defined as such).
Please write your Comment on this Matter
(This will be visible if found suitable):
Name: *
Email (will not be displayed): *
Matter: *
27,7
Enter bigger number from above :*
Home > Coding Tips on Java > RMI Example > RMI Code
Visitor/User submitted related resources:
(Visible upon review and approved by this site Administrator)
Referred By Name *:
Resource URL *: (e.g, URL should be starting with http://www.-----.---)
 
Resource Short Description *:
27,26
Enter bigger number from above : *

Please log in to add or reply to any matter<- requires login
Log in or Register
This List is generated as on 2009-07-12 (YYYY-MM-DD)
#Discuss-these : questions-and-answer : Interview-Questions-on-Java
#Java-and-CompareTo : Comparator-and-Reflection-using-Java : Coding-Tips-on-Java
#key-and-value-pair : Composite-keys-in-Hashtable : Coding-Tips-on-Java
#AWT-using-Java-Technology : Customizing-JTree-using-Swing : Coding-Tips-on-Java
#Event-handling-and-Java-Technology : Event-handling-and-Java-Technology : Coding-Tips-on-Java
#Example-on-using-Final-Keyword-in-Java-code : Using-Final-Keyword : Coding-Tips-on-Java
#Java-Code : In-memory-AWT-Image-Creation : Coding-Tips-on-Java
#Java-Types-with-Bit-size : Java-Integral-Types : Coding-Tips-on-Java
#Using-Java-Technology : Search-Class-in-JAR : Coding-Tips-on-Java
#Drag-and-Drop : Move-component-on-Java-Applet : Coding-Tips-on-Java
#Using-Java-Technology : ArrayBlockingQueue-and-Queue : Coding-Tips-on-Java
#Castor-XSD : Auto-generation-of-code-using-Java : Coding-Tips-on-Java
#Using-Java-Technology : RTF-to-Text-conversion : Coding-Tips-on-Java
#Using-Java-Technology : Common-re-usable-Utility-and-Frameworks : Coding-Tips-on-Java
#Using-Java-Technology : Java-Comparable-and-Comparator : Coding-Tips-on-Java
#Using-Java-Technology : Example-of-Drag-and-Drop : Coding-Tips-on-Java
#Using-Java-Technology : Exception-Hierarchy-and-Handling : Coding-Tips-on-Java
#Using-Java-Technology : Difference-of-Serializable-and-Externalization : Coding-Tips-on-Java
#Coding-Player : Java-MIDI-Music : Coding-Tips-on-Java
#Using-Java-Technology : Object-Oriented-concepts : Coding-Tips-on-Java
#Using-Java-Technology : Java-Object-pass-by-value : Coding-Tips-on-Java
#Questions-and-answer : SAX-Parser-on-Java-Technology : Coding-Tips-on-Java
#Using-Java-Technology : RMI-Stub-and-Skeleton : Coding-Tips-on-Java
#Timer-and-Task-Schedule : Quartz-Scheduler-Questions : Coding-Tips-on-Java
#Using-Java-Technology : Quartz-Scheduler-and-Java-Scheduler : Coding-Tips-on-Java
#Eclispe-and-Using-Code-coverage : Java-Source-coverage-tool : Coding-Tips-on-Java
#Piped-Thread-Communication : Java-IO-Package : Coding-Tips-on-Java
#Code-to-Use : Show-Vertical-Text-on-Graph : Coding-Tips-on-Java
#Decorator-Design : Decorator-Java : Coding-Tips-on-Java
#Java-URL-Path : URL-Relative-Path : Coding-Tips-on-Java
#RMI-Code : RMI-Example : Coding-Tips-on-Java
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved.
CONTACT    PRIVACY POLICY    DISCLAIMER

This web site provides some of the information about various technologies, example 
code, tips, tutorials etc. Like any printed matterials, content of these pages may 
become out of date over a period of time. Therefore all visitor/users of this web 
site are requested/advised to refer to the originating parties/sources for the 
latest changes and happenings for detailed information. This information is not 
intended to be a substitute for the original reference provided by the originating 
parties/sources.