RMI is a specification for the Java platform to achieve remote call. Here is a small example, this machine test passes a total of three Java classes, remote interfaces, server program, client programs remote interface: import java.rmi. *;
public interface HelloIn extends java.rmi.Remote {String sayHello () throws RemoteException;} the server program: import java.rmi *; import java.net *; import java.rmi.registry *; import java.rmi.... Server. *;
public class Hello extends java.rmi.server.UnicastRemoteObject implements HelloIn {public Hello () throws RemoteException {super ();} public String sayHello () throws RemoteException {return "Hello, World!";} public static void main (String [ ] args) {//system.setsecuritymanager (New java.rmi.rmisecuritymanager ()); try {hello h = new hello (); java.rmi.naming.rebind ("Hello", H); System.out.print ("Ready ...");} catch (Exception E) {E.PrintStackTrace ();}}} Start the RMI in the command line mode before executing the server: Start RMIREGISTRY client program: START RMIREGISTRY client program: Import java.rmi. *; import java.rmi.registry. *;
public class Helloworld {public static void main (String [] args) {//System.setProperty ( "java.security.policy", "client.policy"); //System.setSecurityManager(new java.rmi.RMISecurityManager () ); Try {helloin Hi = (Helloin) naming.lookup ("// fengl / hello"); for (int i = 0; i <10; i ) {system.out.println (hi.sayhello ()); }}}} CatCH (Exception E) {E.PrintStackTrace ();}}}
Use RMIC Hello to generate Stub and Skeleton's Class, which is actually an implementation of the bottom layer of remote calls before performing client programs. Finally, the Java HelloWorld console printed Hello, World, successful calls.