Steps in creating a java RMI application
/* MathSrvrInterface.java */ import java.rmi.*; public interface MathSrvrInterface extends Remote { public int square(int n) throws RemoteException; }
/* MathSrvr.java */ import java.rmi.*; import java.rmi.server.*; public class MathSrvr extends UnicastRemoteObject implements MathSrvrInterface { public MathSrvr () throws RemoteException { } public int square(int n) throws RemoteException { return n * n; } }
/* MathSrvrServer.java */ import java.rmi.Naming; public class MathSrvrServer { public static void main (String[] argv) { try { Naming.rebind ("MathSrvr", new MathSrvr ()); System.out.println ("MathSrvr Server is ready."); } catch (Exception e) { System.out.println ("MathSrvr Server failed: " + e); } } }
/* MathSrvrClient.java */ import java.rmi.Naming; public class MathSrvrClient { public static void main (String[] argv) { try { MathSrvrInterface mathsrvr = (MathSrvrInterface) Naming.lookup ("//127.0.0.1/MathSrvr"); System.out.println ("the square of 17 is " + mathsrvr.square(17)); } catch (Exception e) { System.out.println ("MathSrvrClient exception: " + e); } } }
rmic MathSrvr
rmiregistry
java MathSrvrServer
java MathSrvrClient