| |
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 :
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...
|
Written By : Amit
Title :
Search Class in JAR
Description :
Using Java Technology More...
|
Written By : Amit
Title :
Move component on Java Applet
Description :
Drag and Drop More...
|
Written By : Amit
Title :
ArrayBlockingQueue and Queue
Description :
Using Java Technology More...
|
Written By : Amit
Title :
Auto generation of code using Java
Description :
Castor XSD More...
|
| Tags/Keywords : 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 | |
Please be informed that NONE of the design/code from this
page is claiming to be some sort of best practices and we DO NOT expect
any of our visitor/reader of this page to assume this as some sort of
best practice for any context and should not be using this
as it is without appropriate evaluation to their, so to say,
specific programming context.
This page intends only to provide bit and piece of known ways for
doing some sort of example and may not be fit for any other purpose.
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.
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.
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).
|
|
|
Commented By -> laxya | i am creating an lan tool for that would u please help mi...
i am using rmi,swing,in dat i want to acess the remote client resources for administration and also access
the file on next pc's.
|
| | |
Commented By -> mayank rungta | respected sir
i have designed a chat server in java rmi
it is properly working on the computer connected in
lan but
i want to treat my pc as server and a user who have
a internet connection can use my server and can
chat to any body who have my client software
so what should i do |
| | |
Commented By -> Amit | Hello Mayank
I think this question is more towards the network infrastructure
requirement kind of areas, and to my best understanding in order
to run a server in the internet space, one have to have a
static IP(or global IP), but this is my thinking only, you need
to consult somebody who is having hardware and networking knowledge, I think would be better answer this
question.
Hope this help.
|
| | Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Coding Tips on Java >>> RMI Example >>> RMI Code |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
 |
Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example
code, tips, tutorials etc. Like any printed materials, 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.
By accessing and using this website in any ways, including, without
limitation, browsing the website pages, using any information, using any content and/or
downloading any materials, you agree to and are bound by the terms of use
described in this page and Usage Terms and Conditions.
If you do not agree to all of
the terms and conditions contained in the terms of use described in this
page and Usage Terms and Conditions, do not use this
website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use
on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being
taken for providing accurate and up-to-date information but like any printed materials,
these blog(s)/contents may eventually be outdated one day, so if you are using any
of these information, please refer original content/documentation from respective sources.
And under no circumstances shall the Author of these contents and/or this web site
be liable for any loss, damage, expense incurred or suffered which is claimed to have
occurred because of usage of the contents of this web site.
If you have any questions/queries/feedback/suggestions then please write to this web
site owner at contact.
|  |
|
|
|
|
|