SlideShare uma empresa Scribd logo
1 de 33
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

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
 
Java RMI
Java RMIJava RMI
Java RMI
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
 
Java RMI
Java RMIJava RMI
Java RMI
 
Vision of cloud computing
Vision of cloud computingVision of cloud computing
Vision of cloud computing
 
Distributed DBMS - Unit 6 - Query Processing
Distributed DBMS - Unit 6 - Query ProcessingDistributed DBMS - Unit 6 - Query Processing
Distributed DBMS - Unit 6 - Query Processing
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Types of Load distributing algorithm in Distributed System
Types of Load distributing algorithm in Distributed SystemTypes of Load distributing algorithm in Distributed System
Types of Load distributing algorithm in Distributed System
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Servlets
ServletsServlets
Servlets
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
PHP - Introduction to PHP Date and Time Functions
PHP -  Introduction to  PHP Date and Time FunctionsPHP -  Introduction to  PHP Date and Time Functions
PHP - Introduction to PHP Date and Time Functions
 
Event handling
Event handlingEvent handling
Event handling
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
 
Simple object access protocol(soap )
Simple object access protocol(soap )Simple object access protocol(soap )
Simple object access protocol(soap )
 
Distributed Programming using RMI
Distributed Programming using RMIDistributed Programming using RMI
Distributed Programming using RMI
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 

Destaque

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 PresentationMasud 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 TutorialGuo Albert
 
Java - Remote method invocation
Java - Remote method invocationJava - Remote method invocation
Java - Remote method invocationRiccardo Cardin
 
Tutorial passo a passo sobre RMI
Tutorial passo a passo sobre RMITutorial passo a passo sobre RMI
Tutorial passo a passo sobre RMISimão Neto
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method InvocationSonali Parab
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocationDew 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 Examplekamal kotecha
 
Distributes objects and Rmi
Distributes objects and RmiDistributes objects and Rmi
Distributes objects and RmiMayank Jain
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJBPeter 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 connectivityTanmoy Barman
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods pptkamal kotecha
 

Destaque (20)

Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
 
Java RMI Presentation
Java RMI PresentationJava RMI Presentation
Java RMI Presentation
 
RMI
RMIRMI
RMI
 
Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)
 
A Short Java RMI Tutorial
A Short Java RMI TutorialA Short Java RMI Tutorial
A Short Java RMI Tutorial
 
Java rmi
Java rmiJava rmi
Java rmi
 
Tutorial su Java RMI
Tutorial su Java RMITutorial su Java RMI
Tutorial su Java RMI
 
EJB .
EJB .EJB .
EJB .
 
Java - Remote method invocation
Java - Remote method invocationJava - Remote method invocation
Java - Remote method invocation
 
Tutorial passo a passo sobre RMI
Tutorial passo a passo sobre RMITutorial passo a passo sobre RMI
Tutorial passo a passo sobre RMI
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Rmi ppt-2003
Rmi ppt-2003Rmi ppt-2003
Rmi ppt-2003
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
 
Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)
 
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
 
Distributes objects and Rmi
Distributes objects and RmiDistributes objects and Rmi
Distributes objects and Rmi
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
 
Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 

Semelhante a Java rmi example program with code

Semelhante a Java rmi example program with code (20)

Distributed Objects and JAVA
Distributed Objects and JAVADistributed Objects and JAVA
Distributed Objects and JAVA
 
Java RMI
Java RMIJava RMI
Java RMI
 
Remote method invocatiom
Remote method invocatiomRemote method invocatiom
Remote method invocatiom
 
Call Back
Call BackCall Back
Call Back
 
Module 1 Introduction
Module 1   IntroductionModule 1   Introduction
Module 1 Introduction
 
Call Back
Call BackCall Back
Call Back
 
Call Back
Call BackCall Back
Call Back
 
Call Back
Call BackCall Back
Call Back
 
17rmi
17rmi17rmi
17rmi
 
Rmi
RmiRmi
Rmi
 
Rmi
RmiRmi
Rmi
 
Rmi
RmiRmi
Rmi
 
ADB Lab Manual.docx
ADB Lab Manual.docxADB Lab Manual.docx
ADB Lab Manual.docx
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
 
DS
DSDS
DS
 
Java programming concept
Java programming conceptJava programming concept
Java programming concept
 
RMI (Remote Method Invocation)
RMI (Remote Method Invocation)RMI (Remote Method Invocation)
RMI (Remote Method Invocation)
 
CS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUALCS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUAL
 
Please look at the attach See.doc. I am getting this error all th.docx
Please look at the attach See.doc. I am getting this error all th.docxPlease look at the attach See.doc. I am getting this error all th.docx
Please look at the attach See.doc. I am getting this error all th.docx
 
Introduction To Rmi
Introduction To RmiIntroduction To Rmi
Introduction To Rmi
 

Mais de 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 MySqlkamal kotecha
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types pptkamal kotecha
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operatorkamal kotecha
 

Mais de kamal kotecha (17)

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
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
JSP Error handling
JSP Error handlingJSP Error handling
JSP Error handling
 
Jsp element
Jsp elementJsp element
Jsp element
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Interface
InterfaceInterface
Interface
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Class method
Class methodClass method
Class method
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Control statements
Control statementsControl statements
Control statements
 
Jsp myeclipse
Jsp myeclipseJsp myeclipse
Jsp myeclipse
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
 

Último

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Último (20)

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.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