This example contains a total of 5 files, which are HelloClient, Hello, HelloServer, Configur, Config.properties.
/* * CREATED ON 2004-11-10 * * / package test.rmi;
Import java.rmi.remote; import java.rmi.remoteexception;
/ ** * @Author dogcome * *
Remote method call interface, define remote call method p> * / public interface hello extends remote {string Sayello () THROWS RemoteException;
}
/ * HelloServer * // * * CREATED ON 2004-11-10 * * / package test.rmi;
Import java.rmi.remoteException; import java.rmi.registry; import java.rmi.registry.registry; import java.rmi.server.UnicastRemoteObject;
/ ** * @Author dogcome * *
The server is implemented in the Hello interface, and the specific business logic used to implement remote call methods p> * / public class helloserver extends unicastremoteObject imports hello {string name; Public HelloServer (String S) throws remoteException {super (); name = s;}
Public String Sayhello () THROWS RemoteException {// Todo auto-generated method stub return "Hello World!";
Public static void main (String [] args) {/ ** The following sentence is required to add, you need to perform permission, that is, increase the .policy file * and use the following format in the command line * java -djava.security .policy = java.policy test.rmi.helloserver * //*system.setSecurityManager (New RMISecurityManager ()); * / registry regiTry = null; try {/ ** Start the registration server, use this statement no longer need Command line environment * Start the Registry service * / registry = locateregistry.getRegistry (); / * If no connection is not got, this sentence will throw an exception, and after the capture is related. * / Registry.list (); system. Out.println ("Register THE EXIST Server!"); // $ NON-NLS-1 $} catch (transoteexception re) {Try {Int port = integer.parseint ("HelloServer.registryServerPort); // $ NON-NLS-1 $ registry = locateregistry.createRegistry (port); System.out.Println ("CREATE Registry Server!"); // $ NON-NLS-1 $} catch (Exception E) {E. PRINTSTACKTRACE (); } Try {HelloServer HelloServer = New HelloServer ("Hello"); Registry .Rebind ("HelloServer.HelloserverName"), HelloServer); // $ NON-NLS-1 $ SYSTEM.OUT.PRINTLN ("HelloServer Server START! "); // $ NON-NLS-1 $} catch (Exception E) {E.PrintStackTrace ();}}} / * HelloClient * // * * Created on 2004-11-10 * * / package test .rmi;
Import java.rmi. *; import java.rmi.registry; import java.rmi.registry.registry;
/ ** * @Author dogcome * *
client, you need to register the server and use JNDI p> * / public class helloclient {public static void main (string [] args) {/ ** To add, you need to perform permission, that is, increase the .policy file * and use the following format * java -djava.security.policy = java.policy test.rmi.helloserver * / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /*System.SetSecurityManager new RMISecurityManager ()); * / try {/ * registration server * / String hostName = Configur.getString ( "HelloServer.RegistryServerName"); int port = Integer.parseInt (Configur.getString ( "HelloServer.RegistryServerPort")); Registry Registry = locateregistry.getregistry (Hostname, Port);
Hello hello = (Hello) registry.lookup (Configur.getString ( "HelloServer.HelloServerName")); String message = hello.sayHello (); System.out.println (message);} catch (Exception e) {e.printStackTrace ()}}}
/ * * Created on 2004-11-12 * * / package test.rmi;
Import java.util.missingResourceException; import java.util.resourcebundle;
/ ** * @Author dogcome * *
Get the configuration information required for the system run, the corresponding configuration file name is config.properties p> * / public class configur {private static final string bundle_name = "TEST.RMI .config ";
Private static final resourcebundle resource_bundle = resourcebundle .getbundle;
Private configur () {} / ** *
Get the configuration file through the key name p> * @Param Key Key Name * @Return string profile information * / public static string getString (String key) { Try {return resource_bundle.getstring (key);} catch (miss "! ' key '! ';}}} # config.properties
HelloServer.registryServerPort = 1111
HelloServer.helloserverName = HelloServer
HelloServer.registryServerName = 127.0.0.1
First use the RMIC Test.rmi.HelloServer command to generate two types of STUBs and SKELs and run the HelloServer server Java Test.rmi.HelloServer Finally run the HelloClient client Java Test.rmi.HelloClient screen to output HelloWorld, which is the simplest RMI remote call is successful