RMI PROGRAM EXECUTION

//AUTHOR----N.V.RAJA SEKHAR REDDY


                       HI, THIS POST SHOWS EACH AND EVERY STEP FOR THE EXECUTION OF A RMI PROGRAM WITH APPROPRIATE SCREEN SHOTS.



HERE I AM CONSIDERING A CALCULATOR EXAMPLE.



Basically we need four java files for this execution process:

1) Calculator Interface             :Calculator.java

2)Calculator Implementation    :CalculatorImpl.java

3)Calculator  Server                :CalculatorServer.java

4)Calculator Client                  :CalculatorClient.java



Programs for calculator operation:



//file name is: Calculator.java



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

    public long sub(long a, long b)
        throws java.rmi.RemoteException;

    public long mul(long a, long b)
        throws java.rmi.RemoteException;

    public long div(long a, long b)
        throws java.rmi.RemoteException;
}

 


//2.file name is: CalculatorImpl.java



import java.rmi.*;
import javax.rmi.*;
public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject implements Calculator {

    // Implementations must have an
    //explicit constructor
    // in order to declare the
    //RemoteException exception
    public CalculatorImpl()
        throws java.rmi.RemoteException {
        super();
    }

    public long add(long a, long b)
        throws java.rmi.RemoteException {
        return a + b;
    }

    public long sub(long a, long b)
        throws java.rmi.RemoteException {
        return a - b;
    }

    public long mul(long a, long b)
        throws java.rmi.RemoteException {
        return a * b;
    }

    public long div(long a, long b)
        throws java.rmi.RemoteException {
        return a / b;
    }
}






//3.filename is:CalculatorServer.java





import java.rmi.Naming;

public class CalculatorServer {

  public CalculatorServer() {
    try {
      Calculator c = new CalculatorImpl();
      Naming.rebind("rmi://localhost:1099/CalculatorService", c);
    }
catch (Exception e) {
      System.out.println("Trouble: " + e);
    }
  }

  public static void main(String args[]) {
    new CalculatorServer();
  }
}






//4.file name is:CalculatorClient.java





import java.rmi.Naming;

import java.rmi.RemoteException;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.rmi.NotBoundException;



public class CalculatorClient {



    public static void main(String[] args) throws NumberFormatException, IOException {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        try {

            Calculator c = (Calculator)Naming.lookup("rmi://localhost/CalculatorService");

            //System.out.println( c.sub(4, 3) );

            //System.out.println( c.add(4, 5) );

            //System.out.println( c.mul(3, 6) );

            //System.out.println( c.div(9, 3) );

            System.out.println("1-add 2-sub 3-mul 4-div");

            System.out.println("ENTER YOUR CHOICE:");

            int x=Integer.parseInt(br.readLine());

            switch(x)

            {

            case 1:

                System.out.println("enter two numbers:");

                int y=Integer.parseInt(br.readLine());

                int z=Integer.parseInt(br.readLine());

                System.out.println(c.add(y,z));

                  break;

            case 2:

                System.out.println("enter two numbers:");

                 y=Integer.parseInt(br.readLine());

                 z=Integer.parseInt(br.readLine());

                System.out.println(c.sub(y,z));

                 break;

              case 3:

                   {

                System.out.println("enter two numbers:");

                 y=Integer.parseInt(br.readLine());

                 z=Integer.parseInt(br.readLine());

                System.out.println(c.mul(y,z));

                break;

                    }

            case 4:

                System.out.println("enter two numbers:");

                 y=Integer.parseInt(br.readLine());

                 z=Integer.parseInt(br.readLine());

                System.out.println(c.div(y,z));

                break;

              default:

                System.out.println("you havn't entered anything");

                break;

            }

        }

        catch (MalformedURLException murle) {

            System.out.println();

            System.out.println(

              "MalformedURLException");

            System.out.println(murle);

        }

        catch (RemoteException re) {

            System.out.println();

            System.out.println("RemoteException");

            System.out.println(re);

        }

        catch (NotBoundException nbe) {

            System.out.println();

            System.out.println("NotBoundException");

            System.out.println(nbe);

        }

        catch (java.lang.ArithmeticException ae) {

            System.out.println();

            System.out.println(

             "java.lang.ArithmeticException");

            System.out.println(ae);

          

        }

    }

}





EXECUTION PROCEDURE:


 1)For the execution of the rmi program, we need to set the classpath at the environment variables( This at mycomputer properties on windows operating systems)

 





 2)Compile the Calculator Interface and Calculator Implementation programs:



c:\>rmi>javac Calculator.java
c:\>rmi>javac CalculatorImpl.java 


Screen shot:
 
IT CAN EXECUTE SUCCESS FULLY. SOME TIMES YOU CAN FIND AN ERROR
"CAN NOT FIND THE SYMBOL"--> IF YOU ENCOUNTER THIS KIND OF ERROR,THE CLASSPATH  IS NOT PROPERLY SET AT THE ENVIRONMENT VARIABLES


THE ERROR SHOWS LIKE THIS:


3)COMPILE THE IMPLEMENTATION CLASS USING RMI COMPILER (RMIC)


C:\>rmi>rmic CalculatorImpl   ((hit enter))


  THIS GENERATES A "STUB CLASS" AT THE SAME RMI DIRECTORY IN YOUR C DRIVE


4)COMPILE THE CALCULATOR SERVER AND CALCULATOR CLIENT PROGRAMS AT THE CMD PROMPT
(SEE THE ABOVE FIGURE FOR THE TASKS WHAT WE HAVE DONE TILL NOW)


5)START RMI REGISTRY AT CMD PROMPT BY TYPING


C:\>RMI>start rmiregistry  


-- a new window appers on the screen like below --


6) START THE CALCULATOR SERVER BY TYPING THE CMD


C:\>RMI>start java CalculatorServer.java


(again a new window appers like below)




7)NEXT AND LOST STEP IS RUN THE CALCULATORCLIENT


C:\>RMI>java CalculatorClient


(THE OUT PUT WINDOW APPEARS LIKE THIS:)


8) MISSION COMPLETED


 (IF YOU HAVE ANY QUERIES, POST IN COMMENTS)




0 komentar:

Post a Comment

Related Posts with Thumbnails
GiF Pictures, Images and Photos