SlideShare a Scribd company logo
RMI




      http://www.java2all.com
Chapter 2


RMI Program Code and
      Example:


                  http://www.java2all.com
RMI Program Code and
       Example:


                       http://www.java2all.com
CLICK HERE for step by step learning with
description of each step
    To run program of RMI in java is quite
difficult, Here I am going to give you four
different programs in RMI to add two integer
numbers.
    First program is for declare a method in an
interface.
    Second Program is for implementing this
method and logic.
    Third program is for server side.
                                      http://www.java2all.com
And last one is for client side.
     At the last I will give steps to run this
program in one system.


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

public interface Calculator extends Remote
{
  public long add(long a,long b) throws RemoteException;
}




                                                           http://www.java2all.com
CalculatorImpl.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class CalculatorImpl extends UnicastRemoteObject implements Calculator
{
  protected CalculatorImpl() throws RemoteException
  {
    super();
  }
  public long add(long a, long b) throws RemoteException
  {
    return a+b;
  }
}




                                                                                http://www.java2all.com
CalculatorServer.java
import java.rmi.Naming;

public class CalculatorServer
{
  CalculatorServer()
  {
    try
    {
       Calculator c = new CalculatorImpl();
       Naming.rebind("rmi://127.0.0.1:1099/CalculatorService", c);
    }
    catch (Exception e)
    {
       e.printStackTrace();
    }
  }
  public static void main(String[] args)
  {
    new CalculatorServer();
  }
}

                                                                     http://www.java2all.com
CalculatorClient.java
import java.rmi.Naming;

public class CalculatorClient
{
  public static void main(String[] args)
  {
    try
    {
       Calculator c = (Calculator) Naming.lookup("//127.0.0.1:1099/CalculatorService");
       System.out.println("addition : "+c.add(10, 15));
    }
    catch (Exception e)
    {
       System.out.println(e);
    }
  }
}




                                                                                  http://www.java2all.com
Steps to run this programs:

     First of all put these four programs inside
bin folder of JDK.

     As an example suppose our JDK folder is
inside java folder in drive D:

     Now open command prompt and do
following steps.
Cd
d:
                                         http://www.java2all.com
cd Javajdk1.6.0_23bin
 javac Calculator.java
 javac CalculatorImpl.java
 javac CalculatorServer.java
 javac CalculatorClient.java
 rmic CalculatorImpl
 start rmiregistry
 java CalculatorServer
 open another cmd and again go to same path
d:Javajdk1.6.0_23bin
 java CalculatorClient

Output:
                                          http://www.java2all.com
http://www.java2all.com
http://www.java2all.com
http://www.java2all.com
RMI example - code in java -application:




                                http://www.java2all.com
Steps for Developing the RMI Application:

(1) Define the remote interface
(2) Define the class and implement the remote
interface(methods) in this class
(3) Define the Server side class
(4) Define the Client side class
(5) Compile the all four source(java) files
(6) Generate the Stub/Skeleton class by command
(7) Start the RMI remote Registry
(8) Run the Server side class
(9) Run the Client side class(at another JVM)

                                          http://www.java2all.com
(1) Define the remote interface:

      This is an interface in which we are declaring the
methods as per our logic and further these methods
will be called using RMI.

      Here we create a simple calculator application
by RMI so in that we need four methods such as
addition, subtraction, multiplication and division as
per logic.

      so create an interface name Calculator.java and
declare these methods without body as per the
requirement of a simple calculator RMI application.
                                              http://www.java2all.com
Calculator.java:
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Calculator extends Remote
{
  public long addition(long a,long b) throws RemoteException;
  public long subtraction(long a,long b) throws RemoteException;
  public long multiplication(long a,long b) throws RemoteException;
  public long division(long a,long b) throws RemoteException;
}

Note:
       We must extends the Remote interface because
this interface will be called remotely in between the
client and server.
 Note:
       The RemoteException is an exception that can
occur when a failure occur in the RMI process.http://www.java2all.com
(2) Define the class and implement the remote
interface(methods) in this class:

      The next step is to implement the interface so
define a class(CalculatorImpl.java) and implements
the interface(Calculator.java) so now in the class we
must define the body of those methods(addition,
subtraction, multiplication, division) as per the logic
requirement in the RMI application(Simple
Calculator).

     This class run on the remote server.

                                               http://www.java2all.com
CalculatorImpl.java:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class CalculatorImpl extends UnicastRemoteObject implements Calculator
{
  protected CalculatorImpl() throws RemoteException
  {
    super();
  }
  public long addition(long a, long b) throws RemoteException
  {
    return a+b;
  }
  public long subtraction(long a, long b) throws RemoteException
  {
    return a-b;
  }
  public long multiplication(long a, long b) throws RemoteException
  {
    return a*b;
  }


                                                                                http://www.java2all.com
public long division(long a, long b) throws RemoteException
    {
      return a/b;
    }
    public long addition(long a, long b) throws RemoteException
    {
      return a+b;
    }
}



Note:
 
      The UnicastRemoteObject is a base class for 
most user-defined remote objects. The general form of 
this class is, Public class UnicastRemoteObject
extends RemoteServer
 

                                                                   http://www.java2all.com
It supplies the TCP based point-to-point 
references so this class provides some necessary 
services that we need in our application otherwise 
have to implement of interface cannot do ourselves as 
well as can’t access these methods remotely.
 
(3) Define the Server side class:
 
      The server must bind its name to the registry by 
passing the reference link with remote object name. 
For that here we are going to use rebind method which 
has two arguments:

                                            http://www.java2all.com
The first parameter is a URL to a registry that 
includes the name of the application and The second 
parameter is an object name that is access remotely in 
between the client and server.
 
      This rebind method is a method of the Naming 
class which is available in the java.rmi.* package.
 
      The server name is specified in URL as a 
application name and here the name is 
CalculatorService in our application.
 

                                             http://www.java2all.com
Note:
 
The general form of the URL:
 
rmi://localhost:port/application_name

       Here, 1099 is the default RMI port and 127.0.0.1 
is a localhost-ip address.
 
CalculatorServer.java:



                                             http://www.java2all.com
import java.rmi.Naming;

public class CalculatorServer
{
  CalculatorServer()
  {
    try
    {
       Calculator c = new CalculatorImpl();
       Naming.rebind("rmi://localhost:1099/CalculatorService", c);
    }
    catch (Exception e)
    {
       System.out.println(“Exception is : ”+e);
    }
  }
  public static void main(String[] args)
  {
    new CalculatorServer();
  }
}




                                                                     http://www.java2all.com
(4) Define the Client side class:

       To access an object remotely by client side that 
is already bind at a server side by one reference URL 
we use the lookup method which has one argument 
that is a same reference URL as already applied at 
server side class.
 
       This lookup method is a method of the Naming 
class which is available in the java.rmi.* package.
 


                                              http://www.java2all.com
The name specified in the URL must be exactly 
match the name that the server has bound to the 
registry in server side class and here the name is 
CalculatorService.
       
      After getting an object we can call all the 
methods which already declared in the interface 
Calculator or already defined in the class 
CalculatorImpl by this remote object.




                                           http://www.java2all.com
CalculatorClient.java:
import java.rmi.Naming;

public class CalculatorClient
{
  public static void main(String[] args)
  {
    try
    {
       Calculator c = (Calculator) Naming.lookup("//127.0.0.1:1099/CalculatorService");
       System.out.println("Addition : "+c.addition(10,5));
       System.out.println("Subtraction : "+c.subtraction(10,5));
       System.out.println("Multiplication :"+c.multiplication(10,5));
System.out.println("Division : "+c. division(10,5));
    }
    catch (Exception e)
    {
       System.out.println(“Exception is : ”+e);
    }
  }
}



                                                                                  http://www.java2all.com
(5) Compile the all four source(java) files:
 
javac Calculator.java
javac CalculatorImpl.java
javac CalculatorClient.java
javac CalculatorServer.java
 
After compiled, in the folder we can see the four class 
files such as
 
Calculator.class
CalculatorImpl.class
CalculatorClient.class
CalculatorServer.class                        http://www.java2all.com
(6) Generate the Stub/Skeleton class by command:
 
      There is a command rmic by which we can 
generate a Stub/Skeleton class.
 
Syntax:
 rmic class_name
 
      Here the class_name is a java file in which the 
all methods are defined so in this application the class 
name is  CalculatorImpl.java file.


                                              http://www.java2all.com
Example:
rmic  CalculatorImpl
 
The above command produce the 
“CalculatorImpl_Stub.class” file.
(7) Start the RMI remote Registry:
 
       The references of the objects are registered into 
the RMI Registry So now you need to start the RMI 
registry for that use the command
 start rmiregistry
       So the system will open rmiregistry.exe (like a 
blank command prompt)
                                               http://www.java2all.com
(8) Run the Server side class:
 
     Now you need to run the RMI Server class.
Here CalculatorServer.java file is a working as a 
Server so run this fie.
 
Java CalculatorServer

(9) Run the Client side class(at another JVM):
 
      Now open a new command prompt for the client 
because current command prompt working as a server 
and finally run the RMI client class.
                                           http://www.java2all.com
Here CalculatorClient.java file is a working as a 
Client so finally run this fie.
 
Java CalculatorClient

Get the output like
 
Addition : 15
Subtraction : 5
Multiplication : 50
Division : 2
 

                                             http://www.java2all.com
NOTE:
 
      For compile or run all the file from command 
prompt and also use the different commands like 
javac, java, start, rmic etc you need to set the class 
path or copy all the java files in bin folder of JDK.
CLICK HERE for simple addition program of RMI in 
java
 




                                             http://www.java2all.com

More Related Content

PPS
Java rmi
PPTX
Java Beans
PPTX
Web services
PPTX
object oriented methodologies
PPTX
Session bean
PPSX
Entity beans in java
PPTX
Remote Method Innovation (RMI) In JAVA
PDF
Remote Method Invocation (RMI)
Java rmi
Java Beans
Web services
object oriented methodologies
Session bean
Entity beans in java
Remote Method Innovation (RMI) In JAVA
Remote Method Invocation (RMI)

What's hot (20)

PDF
Servlet and servlet life cycle
PPT
Java Socket Programming
PPTX
Jdbc ppt
PPTX
Multiple inheritance possible in Java
PDF
Srs template ieee-movie recommender
PPTX
Multithread Programing in Java
PPTX
Eucalyptus, Nimbus & OpenNebula
PPTX
Task programming
PPTX
Presentation on Railway Reservation System
PPT
Servlet life cycle
PPT
Chap2 2 1
PPTX
Cloud Computing
PPTX
Simple object access protocol(soap )
PPTX
Java - Sockets
PPT
Java: GUI
PPTX
Ado.Net Tutorial
PPTX
03 Data Mining Techniques
PPTX
Uml restaurant (group 1)
PPSX
JDBC: java DataBase connectivity
Servlet and servlet life cycle
Java Socket Programming
Jdbc ppt
Multiple inheritance possible in Java
Srs template ieee-movie recommender
Multithread Programing in Java
Eucalyptus, Nimbus & OpenNebula
Task programming
Presentation on Railway Reservation System
Servlet life cycle
Chap2 2 1
Cloud Computing
Simple object access protocol(soap )
Java - Sockets
Java: GUI
Ado.Net Tutorial
03 Data Mining Techniques
Uml restaurant (group 1)
JDBC: java DataBase connectivity
Ad

Viewers also liked (20)

PPTX
Java RMI Presentation
PPT
PPTX
Remote Method Invocation (Java RMI)
PPT
A Short Java RMI Tutorial
PPSX
Java rmi
PDF
Tutorial su Java RMI
PPTX
Java RMI
PPTX
Network programming in java - PPT
PPT
PPTX
Java - Remote method invocation
PDF
Tutorial passo a passo sobre RMI
DOCX
Remote Method Invocation
PDF
Rmi ppt-2003
PPTX
Remote method invocation
PDF
Tutorial su JMS (Java Message Service)
PPS
Java Hibernate Programming with Architecture Diagram and Example
PPT
Distributes objects and Rmi
PDF
Enterprise Java Beans - EJB
PDF
Introduction to Remote Method Invocation (RMI)
PPT
Java servlet life cycle - methods ppt
Java RMI Presentation
Remote Method Invocation (Java RMI)
A Short Java RMI Tutorial
Java rmi
Tutorial su Java RMI
Java RMI
Network programming in java - PPT
Java - Remote method invocation
Tutorial passo a passo sobre RMI
Remote Method Invocation
Rmi ppt-2003
Remote method invocation
Tutorial su JMS (Java Message Service)
Java Hibernate Programming with Architecture Diagram and Example
Distributes objects and Rmi
Enterprise Java Beans - EJB
Introduction to Remote Method Invocation (RMI)
Java servlet life cycle - methods ppt
Ad

Similar to Java rmi example program with code (20)

PPT
Distributed Objects and JAVA
PPT
Java RMI
PPTX
Remote method invocatiom
PPT
Module 1 Introduction
PPT
Call Back
PPT
Call Back
PPT
Call Back
PPT
Call Back
PDF
17rmi
PPT
PPT
PPTX
DOCX
ADB Lab Manual.docx
DOCX
remote method invocation
PDF
JavaRMI, JAVA RPC , INTRO , DESCRIPTION , EXPLAINED.pdf
PPTX
PPT
Java programming concept
PDF
RMI (Remote Method Invocation)
DOC
CS2309 JAVA LAB MANUAL
DOCX
Please look at the attach See.doc. I am getting this error all th.docx
Distributed Objects and JAVA
Java RMI
Remote method invocatiom
Module 1 Introduction
Call Back
Call Back
Call Back
Call Back
17rmi
ADB Lab Manual.docx
remote method invocation
JavaRMI, JAVA RPC , INTRO , DESCRIPTION , EXPLAINED.pdf
Java programming concept
RMI (Remote Method Invocation)
CS2309 JAVA LAB MANUAL
Please look at the attach See.doc. I am getting this error all th.docx

More from kamal kotecha (17)

PPS
Jdbc example program with access and MySql
PPS
Jdbc api
PPS
Jdbc architecture and driver types ppt
PPS
Java Exception handling
PPS
JSP Error handling
PPS
Jsp element
PPS
Jsp chapter 1
PPS
String and string buffer
PPS
Wrapper class
PPS
Packages and inbuilt classes of java
PPS
Interface
PPS
Inheritance chepter 7
PPS
Class method
PPS
Introduction to class in java
PPS
Control statements
PPTX
Jsp myeclipse
PPTX
basic core java up to operator
Jdbc example program with access and MySql
Jdbc api
Jdbc architecture and driver types ppt
Java Exception handling
JSP Error handling
Jsp element
Jsp chapter 1
String and string buffer
Wrapper class
Packages and inbuilt classes of java
Interface
Inheritance chepter 7
Class method
Introduction to class in java
Control statements
Jsp myeclipse
basic core java up to operator

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Institutional Correction lecture only . . .
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Basic Mud Logging Guide for educational purpose
PDF
01-Introduction-to-Information-Management.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
Supply Chain Operations Speaking Notes -ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Microbial diseases, their pathogenesis and prophylaxis
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Institutional Correction lecture only . . .
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Renaissance Architecture: A Journey from Faith to Humanism
human mycosis Human fungal infections are called human mycosis..pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Basic Mud Logging Guide for educational purpose
01-Introduction-to-Information-Management.pdf
O7-L3 Supply Chain Operations - ICLT Program
102 student loan defaulters named and shamed – Is someone you know on the list?
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
O5-L3 Freight Transport Ops (International) V1.pdf

Java rmi example program with code

  • 1. RMI http://www.java2all.com
  • 2. Chapter 2 RMI Program Code and Example: http://www.java2all.com
  • 3. RMI Program Code and Example: http://www.java2all.com
  • 4. CLICK HERE for step by step learning with description of each step To run program of RMI in java is quite difficult, Here I am going to give you four different programs in RMI to add two integer numbers. First program is for declare a method in an interface. Second Program is for implementing this method and logic. Third program is for server side. http://www.java2all.com
  • 5. And last one is for client side. At the last I will give steps to run this program in one system. Calculator.java import java.rmi.Remote; import java.rmi.RemoteException; public interface Calculator extends Remote { public long add(long a,long b) throws RemoteException; } http://www.java2all.com
  • 6. CalculatorImpl.java import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class CalculatorImpl extends UnicastRemoteObject implements Calculator { protected CalculatorImpl() throws RemoteException { super(); } public long add(long a, long b) throws RemoteException { return a+b; } } http://www.java2all.com
  • 7. CalculatorServer.java import java.rmi.Naming; public class CalculatorServer { CalculatorServer() { try { Calculator c = new CalculatorImpl(); Naming.rebind("rmi://127.0.0.1:1099/CalculatorService", c); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new CalculatorServer(); } } http://www.java2all.com
  • 8. CalculatorClient.java import java.rmi.Naming; public class CalculatorClient { public static void main(String[] args) { try { Calculator c = (Calculator) Naming.lookup("//127.0.0.1:1099/CalculatorService"); System.out.println("addition : "+c.add(10, 15)); } catch (Exception e) { System.out.println(e); } } } http://www.java2all.com
  • 9. Steps to run this programs: First of all put these four programs inside bin folder of JDK. As an example suppose our JDK folder is inside java folder in drive D: Now open command prompt and do following steps. Cd d: http://www.java2all.com
  • 10. cd Javajdk1.6.0_23bin javac Calculator.java javac CalculatorImpl.java javac CalculatorServer.java javac CalculatorClient.java rmic CalculatorImpl start rmiregistry java CalculatorServer open another cmd and again go to same path d:Javajdk1.6.0_23bin java CalculatorClient Output: http://www.java2all.com
  • 14. RMI example - code in java -application: http://www.java2all.com
  • 15. Steps for Developing the RMI Application: (1) Define the remote interface (2) Define the class and implement the remote interface(methods) in this class (3) Define the Server side class (4) Define the Client side class (5) Compile the all four source(java) files (6) Generate the Stub/Skeleton class by command (7) Start the RMI remote Registry (8) Run the Server side class (9) Run the Client side class(at another JVM) http://www.java2all.com
  • 16. (1) Define the remote interface: This is an interface in which we are declaring the methods as per our logic and further these methods will be called using RMI. Here we create a simple calculator application by RMI so in that we need four methods such as addition, subtraction, multiplication and division as per logic. so create an interface name Calculator.java and declare these methods without body as per the requirement of a simple calculator RMI application. http://www.java2all.com
  • 17. Calculator.java: import java.rmi.Remote; import java.rmi.RemoteException; public interface Calculator extends Remote { public long addition(long a,long b) throws RemoteException; public long subtraction(long a,long b) throws RemoteException; public long multiplication(long a,long b) throws RemoteException; public long division(long a,long b) throws RemoteException; } Note: We must extends the Remote interface because this interface will be called remotely in between the client and server. Note: The RemoteException is an exception that can occur when a failure occur in the RMI process.http://www.java2all.com
  • 18. (2) Define the class and implement the remote interface(methods) in this class: The next step is to implement the interface so define a class(CalculatorImpl.java) and implements the interface(Calculator.java) so now in the class we must define the body of those methods(addition, subtraction, multiplication, division) as per the logic requirement in the RMI application(Simple Calculator). This class run on the remote server. http://www.java2all.com
  • 19. CalculatorImpl.java: import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class CalculatorImpl extends UnicastRemoteObject implements Calculator { protected CalculatorImpl() throws RemoteException { super(); } public long addition(long a, long b) throws RemoteException { return a+b; } public long subtraction(long a, long b) throws RemoteException { return a-b; } public long multiplication(long a, long b) throws RemoteException { return a*b; } http://www.java2all.com
  • 20. public long division(long a, long b) throws RemoteException { return a/b; } public long addition(long a, long b) throws RemoteException { return a+b; } } Note:   The UnicastRemoteObject is a base class for  most user-defined remote objects. The general form of  this class is, Public class UnicastRemoteObject extends RemoteServer   http://www.java2all.com
  • 21. It supplies the TCP based point-to-point  references so this class provides some necessary  services that we need in our application otherwise  have to implement of interface cannot do ourselves as  well as can’t access these methods remotely.   (3) Define the Server side class:   The server must bind its name to the registry by  passing the reference link with remote object name.  For that here we are going to use rebind method which  has two arguments: http://www.java2all.com
  • 22. The first parameter is a URL to a registry that  includes the name of the application and The second  parameter is an object name that is access remotely in  between the client and server.   This rebind method is a method of the Naming  class which is available in the java.rmi.* package.   The server name is specified in URL as a  application name and here the name is  CalculatorService in our application.   http://www.java2all.com
  • 23. Note:   The general form of the URL:   rmi://localhost:port/application_name Here, 1099 is the default RMI port and 127.0.0.1  is a localhost-ip address.   CalculatorServer.java: http://www.java2all.com
  • 24. import java.rmi.Naming; public class CalculatorServer { CalculatorServer() { try { Calculator c = new CalculatorImpl(); Naming.rebind("rmi://localhost:1099/CalculatorService", c); } catch (Exception e) { System.out.println(“Exception is : ”+e); } } public static void main(String[] args) { new CalculatorServer(); } } http://www.java2all.com
  • 25. (4) Define the Client side class: To access an object remotely by client side that  is already bind at a server side by one reference URL  we use the lookup method which has one argument  that is a same reference URL as already applied at  server side class.   This lookup method is a method of the Naming  class which is available in the java.rmi.* package.   http://www.java2all.com
  • 26. The name specified in the URL must be exactly  match the name that the server has bound to the  registry in server side class and here the name is  CalculatorService.   After getting an object we can call all the  methods which already declared in the interface  Calculator or already defined in the class  CalculatorImpl by this remote object. http://www.java2all.com
  • 27. CalculatorClient.java: import java.rmi.Naming; public class CalculatorClient { public static void main(String[] args) { try { Calculator c = (Calculator) Naming.lookup("//127.0.0.1:1099/CalculatorService"); System.out.println("Addition : "+c.addition(10,5)); System.out.println("Subtraction : "+c.subtraction(10,5)); System.out.println("Multiplication :"+c.multiplication(10,5)); System.out.println("Division : "+c. division(10,5)); } catch (Exception e) { System.out.println(“Exception is : ”+e); } } } http://www.java2all.com
  • 28. (5) Compile the all four source(java) files:   javac Calculator.java javac CalculatorImpl.java javac CalculatorClient.java javac CalculatorServer.java   After compiled, in the folder we can see the four class  files such as   Calculator.class CalculatorImpl.class CalculatorClient.class CalculatorServer.class http://www.java2all.com
  • 29. (6) Generate the Stub/Skeleton class by command:   There is a command rmic by which we can  generate a Stub/Skeleton class.   Syntax:  rmic class_name   Here the class_name is a java file in which the  all methods are defined so in this application the class  name is  CalculatorImpl.java file. http://www.java2all.com
  • 30. Example: rmic  CalculatorImpl   The above command produce the  “CalculatorImpl_Stub.class” file. (7) Start the RMI remote Registry:   The references of the objects are registered into  the RMI Registry So now you need to start the RMI  registry for that use the command  start rmiregistry   So the system will open rmiregistry.exe (like a  blank command prompt) http://www.java2all.com
  • 31. (8) Run the Server side class:   Now you need to run the RMI Server class. Here CalculatorServer.java file is a working as a  Server so run this fie.   Java CalculatorServer (9) Run the Client side class(at another JVM):   Now open a new command prompt for the client  because current command prompt working as a server  and finally run the RMI client class.   http://www.java2all.com
  • 33. NOTE:   For compile or run all the file from command  prompt and also use the different commands like  javac, java, start, rmic etc you need to set the class  path or copy all the java files in bin folder of JDK. CLICK HERE for simple addition program of RMI in  java   http://www.java2all.com