SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Remote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVARemote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVAPrankit Mishra
 
Chapter 4 a interprocess communication
Chapter 4 a interprocess communicationChapter 4 a interprocess communication
Chapter 4 a interprocess communicationAbDul ThaYyal
 
Remote Procedure Call in Distributed System
Remote Procedure Call in Distributed SystemRemote Procedure Call in Distributed System
Remote Procedure Call in Distributed SystemPoojaBele1
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket ProgrammingVipin Yadav
 
SQL INJECTION
SQL INJECTIONSQL INJECTION
SQL INJECTIONAnoop T
 
Intruders and Viruses in Network Security NS9
Intruders and Viruses in Network Security NS9Intruders and Viruses in Network Security NS9
Intruders and Viruses in Network Security NS9koolkampus
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycleDhruvin Nakrani
 
Security in distributed systems
Security in distributed systems Security in distributed systems
Security in distributed systems Haitham Ahmed
 
Distributed System-Multicast & Indirect communication
Distributed System-Multicast & Indirect communicationDistributed System-Multicast & Indirect communication
Distributed System-Multicast & Indirect communicationMNM Jain Engineering College
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivityTanmoy Barman
 

Was ist angesagt? (20)

Java rmi
Java rmiJava rmi
Java rmi
 
Rmi presentation
Rmi presentationRmi presentation
Rmi presentation
 
Remote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVARemote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVA
 
Chapter 4 a interprocess communication
Chapter 4 a interprocess communicationChapter 4 a interprocess communication
Chapter 4 a interprocess communication
 
Remote Procedure Call in Distributed System
Remote Procedure Call in Distributed SystemRemote Procedure Call in Distributed System
Remote Procedure Call in Distributed System
 
Message passing in Distributed Computing Systems
Message passing in Distributed Computing SystemsMessage passing in Distributed Computing Systems
Message passing in Distributed Computing Systems
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Servlets
ServletsServlets
Servlets
 
Rmi architecture
Rmi architectureRmi architecture
Rmi architecture
 
SQL INJECTION
SQL INJECTIONSQL INJECTION
SQL INJECTION
 
6.Distributed Operating Systems
6.Distributed Operating Systems6.Distributed Operating Systems
6.Distributed Operating Systems
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Intruders and Viruses in Network Security NS9
Intruders and Viruses in Network Security NS9Intruders and Viruses in Network Security NS9
Intruders and Viruses in Network Security NS9
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Port scanning
Port scanningPort scanning
Port scanning
 
Security in distributed systems
Security in distributed systems Security in distributed systems
Security in distributed systems
 
Distributed System-Multicast & Indirect communication
Distributed System-Multicast & Indirect communicationDistributed System-Multicast & Indirect communication
Distributed System-Multicast & Indirect communication
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 

Andere mochten auch

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
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPTkamal kotecha
 
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
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods pptkamal kotecha
 
Java Servlets
Java ServletsJava Servlets
Java ServletsNitin Pai
 
Transaction management
Transaction managementTransaction management
Transaction managementrenuka_a
 
Java OO Middleware - JEE / EJB / RMI
Java OO Middleware - JEE / EJB / RMIJava OO Middleware - JEE / EJB / RMI
Java OO Middleware - JEE / EJB / RMIYitzhak Stone
 

Andere mochten auch (20)

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
 
Tutorial su Java RMI
Tutorial su Java RMITutorial su Java RMI
Tutorial su Java RMI
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
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)
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
Transaction management
Transaction managementTransaction management
Transaction management
 
Java OO Middleware - JEE / EJB / RMI
Java OO Middleware - JEE / EJB / RMIJava OO Middleware - JEE / EJB / RMI
Java OO Middleware - JEE / EJB / RMI
 

Ähnlich wie Java rmi example program with code

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 

Kürzlich hochgeladen (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 

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