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

What's hot (20)

PPTX
Java RMI
Prajakta Nimje
 
PPTX
Advance Java Topics (J2EE)
slire
 
PDF
Android intents
Siva Ramakrishna kv
 
PPTX
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
PPTX
Fragment
nationalmobileapps
 
PPTX
Ado.Net Tutorial
prabhu rajendran
 
PPT
Java collections concept
kumar gaurav
 
PPTX
android sqlite
Deepa Rani
 
PPT
Jsp ppt
Vikas Jagtap
 
PPS
Java rmi
kamal kotecha
 
PPTX
Servletarchitecture,lifecycle,get,post
vamsi krishna
 
PPTX
Jdbc ppt
sandeep54552
 
PPTX
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
Vigneshkumar Ponnusamy
 
PPT
Java Networking
Sunil OS
 
PPTX
Method Overloading in Java
Sonya Akter Rupa
 
PPS
Jsp element
kamal kotecha
 
PDF
Android resource
Krazy Koder
 
Java RMI
Prajakta Nimje
 
Advance Java Topics (J2EE)
slire
 
Android intents
Siva Ramakrishna kv
 
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
Ado.Net Tutorial
prabhu rajendran
 
Java collections concept
kumar gaurav
 
android sqlite
Deepa Rani
 
Jsp ppt
Vikas Jagtap
 
Java rmi
kamal kotecha
 
Servletarchitecture,lifecycle,get,post
vamsi krishna
 
Jdbc ppt
sandeep54552
 
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
Vigneshkumar Ponnusamy
 
Java Networking
Sunil OS
 
Method Overloading in Java
Sonya Akter Rupa
 
Jsp element
kamal kotecha
 
Android resource
Krazy Koder
 

Viewers also liked (20)

PDF
Remote Method Invocation (RMI)
Peter R. Egli
 
PPTX
Java RMI Presentation
Masud Rahman
 
PPT
RMI
Aravind Nair
 
PPTX
Remote Method Invocation (Java RMI)
Sonali Parab
 
PPT
A Short Java RMI Tutorial
Guo Albert
 
PPSX
Java rmi
Tanmoy Barman
 
PDF
Tutorial su Java RMI
Federico Paparoni
 
PPTX
Java RMI
Ankit Desai
 
PPTX
Network programming in java - PPT
kamal kotecha
 
PPT
EJB .
ayyagari.vinay
 
PPTX
Java - Remote method invocation
Riccardo Cardin
 
PDF
Tutorial passo a passo sobre RMI
Simão Neto
 
DOCX
Remote Method Invocation
Sonali Parab
 
PDF
Rmi ppt-2003
kalaranjani1990
 
PPTX
Remote method invocation
Dew Shishir
 
PDF
Tutorial su JMS (Java Message Service)
Federico Paparoni
 
PPS
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
PPT
Distributes objects and Rmi
Mayank Jain
 
PDF
Enterprise Java Beans - EJB
Peter R. Egli
 
PDF
Introduction to Remote Method Invocation (RMI)
eLink Business Innovations
 
Remote Method Invocation (RMI)
Peter R. Egli
 
Java RMI Presentation
Masud Rahman
 
Remote Method Invocation (Java RMI)
Sonali Parab
 
A Short Java RMI Tutorial
Guo Albert
 
Java rmi
Tanmoy Barman
 
Tutorial su Java RMI
Federico Paparoni
 
Java RMI
Ankit Desai
 
Network programming in java - PPT
kamal kotecha
 
Java - Remote method invocation
Riccardo Cardin
 
Tutorial passo a passo sobre RMI
Simão Neto
 
Remote Method Invocation
Sonali Parab
 
Rmi ppt-2003
kalaranjani1990
 
Remote method invocation
Dew Shishir
 
Tutorial su JMS (Java Message Service)
Federico Paparoni
 
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
Distributes objects and Rmi
Mayank Jain
 
Enterprise Java Beans - EJB
Peter R. Egli
 
Introduction to Remote Method Invocation (RMI)
eLink Business Innovations
 
Ad

Similar to Java rmi example program with code (20)

DOCX
ADB Lab Manual.docx
SaiKumarPrajapathi
 
PPTX
Remote method invocation
Veni7
 
PDF
RMI Java Programming Lab Manual 2019
Gebreigziabher Ab
 
DOCX
remote method invocation
Arun Nair
 
PDF
Run rmi
jdkkamal
 
PDF
JavaRMI, JAVA RPC , INTRO , DESCRIPTION , EXPLAINED.pdf
ishankumra13579
 
PDF
Remote method invocation (as part of the the PTT lecture)
Ralf Laemmel
 
PDF
Remote Method Invocation in JAVA
Jalpesh Vasa
 
PPTX
Introduction To Rmi
SwarupKulkarni
 
PPTX
DS
Verma Mukesh
 
PDF
17rmi
Adil Jafri
 
PPTX
Rmi
Jafar Nesargi
 
PDF
Java rmi
Fazlur Rahman
 
PPTX
Rmi
Vijay Kiran
 
PPT
Java remote method invocation
Van Dawn
 
PPT
Remote Method Invocation
Paul Pajo
 
PPTX
#5 (Remote Method Invocation)
Ghadeer AlHasan
 
PDF
Remote Method Invocation, Advanced programming
Gera Paulos
 
ADB Lab Manual.docx
SaiKumarPrajapathi
 
Remote method invocation
Veni7
 
RMI Java Programming Lab Manual 2019
Gebreigziabher Ab
 
remote method invocation
Arun Nair
 
Run rmi
jdkkamal
 
JavaRMI, JAVA RPC , INTRO , DESCRIPTION , EXPLAINED.pdf
ishankumra13579
 
Remote method invocation (as part of the the PTT lecture)
Ralf Laemmel
 
Remote Method Invocation in JAVA
Jalpesh Vasa
 
Introduction To Rmi
SwarupKulkarni
 
17rmi
Adil Jafri
 
Java rmi
Fazlur Rahman
 
Java remote method invocation
Van Dawn
 
Remote Method Invocation
Paul Pajo
 
#5 (Remote Method Invocation)
Ghadeer AlHasan
 
Remote Method Invocation, Advanced programming
Gera Paulos
 
Ad

More from kamal kotecha (17)

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

Recently uploaded (20)

PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PPTX
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
Different types of inheritance in odoo 18
Celine George
 
PPTX
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Horarios de distribución de agua en julio
pegazohn1978
 
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
Controller Request and Response in Odoo18
Celine George
 
Different types of inheritance in odoo 18
Celine George
 
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 

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