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)

Java RMI
Java RMIJava RMI
Java RMI
Ankit Desai
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
Ronak Chhajed
 
Exception handling
Exception handlingException handling
Exception handling
PhD Research Scholar
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
Somya Bagai
 
Servlet
Servlet Servlet
Servlet
Dhara Joshi
 
Applets
AppletsApplets
Applets
Prabhakaran V M
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
JVM
JVMJVM
JVM
baabtra.com - No. 1 supplier of quality freshers
 
Polymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh SarkarPolymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh Sarkar
Animesh Sarkar
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
Hitesh-Java
 
Introduction to java (revised)
Introduction to java (revised)Introduction to java (revised)
Introduction to java (revised)
Sujit Majety
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Java Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | EdurekaJava Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | Edureka
Edureka!
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
Vipin Yadav
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design Patterns
Anton Keks
 

Viewers also liked (20)

Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
Peter R. Egli
 
Java RMI Presentation
Java RMI PresentationJava RMI Presentation
Java RMI Presentation
Masud Rahman
 
RMI
RMIRMI
RMI
Aravind Nair
 
Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)
Sonali Parab
 
A Short Java RMI Tutorial
A Short Java RMI TutorialA Short Java RMI Tutorial
A Short Java RMI Tutorial
Guo Albert
 
Java rmi
Java rmiJava rmi
Java rmi
Tanmoy Barman
 
Tutorial su Java RMI
Tutorial su Java RMITutorial su Java RMI
Tutorial su Java RMI
Federico Paparoni
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
kamal kotecha
 
EJB .
EJB .EJB .
EJB .
ayyagari.vinay
 
Java - Remote method invocation
Java - Remote method invocationJava - Remote method invocation
Java - Remote method invocation
Riccardo Cardin
 
Tutorial passo a passo sobre RMI
Tutorial passo a passo sobre RMITutorial passo a passo sobre RMI
Tutorial passo a passo sobre RMI
Simão Neto
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
Sonali Parab
 
Rmi ppt-2003
Rmi ppt-2003Rmi ppt-2003
Rmi ppt-2003
kalaranjani1990
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Dew Shishir
 
Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)
Federico Paparoni
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
Distributes objects and Rmi
Distributes objects and RmiDistributes objects and Rmi
Distributes objects and Rmi
Mayank Jain
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
Peter R. Egli
 
Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)
eLink Business Innovations
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
Peter R. Egli
 
Java RMI Presentation
Java RMI PresentationJava RMI Presentation
Java RMI Presentation
Masud Rahman
 
Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)
Sonali Parab
 
A Short Java RMI Tutorial
A Short Java RMI TutorialA Short Java RMI Tutorial
A Short Java RMI Tutorial
Guo Albert
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
kamal kotecha
 
Java - Remote method invocation
Java - Remote method invocationJava - Remote method invocation
Java - Remote method invocation
Riccardo Cardin
 
Tutorial passo a passo sobre RMI
Tutorial passo a passo sobre RMITutorial passo a passo sobre RMI
Tutorial passo a passo sobre RMI
Simão Neto
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
Sonali Parab
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Dew Shishir
 
Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)
Federico Paparoni
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
Distributes objects and Rmi
Distributes objects and RmiDistributes objects and Rmi
Distributes objects and Rmi
Mayank Jain
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
Peter R. Egli
 
Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)
eLink Business Innovations
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Ad

Similar to Java rmi example program with code (20)

ADB Lab Manual.docx
ADB Lab Manual.docxADB Lab Manual.docx
ADB Lab Manual.docx
SaiKumarPrajapathi
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Veni7
 
RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019
Gebreigziabher Ab
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
Arun Nair
 
Run rmi
Run rmiRun rmi
Run rmi
jdkkamal
 
Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)
Ralf Laemmel
 
Introduction To Rmi
Introduction To RmiIntroduction To Rmi
Introduction To Rmi
SwarupKulkarni
 
DS
DSDS
DS
Verma Mukesh
 
17rmi
17rmi17rmi
17rmi
Adil Jafri
 
Rmi
RmiRmi
Rmi
Jafar Nesargi
 
Java rmi
Java rmiJava rmi
Java rmi
Fazlur Rahman
 
Rmi3
Rmi3Rmi3
Rmi3
John Thiagarajan
 
Rmi
RmiRmi
Rmi
Object-Frontier Software Pvt. Ltd
 
Rmi
RmiRmi
Rmi
Vijay Kiran
 
Java remote method invocation
Java remote method invocationJava remote method invocation
Java remote method invocation
Van Dawn
 
#5 (Remote Method Invocation)
#5 (Remote Method Invocation)#5 (Remote Method Invocation)
#5 (Remote Method Invocation)
Ghadeer AlHasan
 
Remote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programmingRemote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programming
Gera Paulos
 
Rmi
RmiRmi
Rmi
vantinhkhuc
 
Rmi
RmiRmi
Rmi
phanleson
 
Rmi
RmiRmi
Rmi
leminhvuong
 
Ad

More from kamal kotecha (18)

Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
kamal kotecha
 
Jdbc example program with access and MySql
Jdbc example program with access and MySqlJdbc example program with access and MySql
Jdbc example program with access and MySql
kamal kotecha
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
kamal kotecha
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
JSP Error handling
JSP Error handlingJSP Error handling
JSP Error handling
kamal kotecha
 
Jsp element
Jsp elementJsp element
Jsp element
kamal kotecha
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
kamal kotecha
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
kamal kotecha
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Class method
Class methodClass method
Class method
kamal kotecha
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
Control statements
Control statementsControl statements
Control statements
kamal kotecha
 
Jsp myeclipse
Jsp myeclipseJsp myeclipse
Jsp myeclipse
kamal kotecha
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
kamal kotecha
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
kamal kotecha
 
Jdbc example program with access and MySql
Jdbc example program with access and MySqlJdbc example program with access and MySql
Jdbc example program with access and MySql
kamal kotecha
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
kamal kotecha
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
kamal kotecha
 

Recently uploaded (20)

THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...
EduSkills OECD
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...
EduSkills OECD
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 

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