From: http://blog.csdn.net/ladofwind/archive/2005/01/11/248820.aspx
RMI is the specification for remote calls on the Java platform. The following is a small example.
There are three Java classes, remote interfaces, ServerProgram, Client program
Remote interface:
Import java. RMI .*;
Public interface helloin extends java. RMI. Remote {
String sayhello () throws RemoteException;
}
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 ();
}
}
}
Run the RMI registration program start rmiregistry in the command line mode before running the server program.
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 ();
}
}
}
Before executing the client program, use rmic hello to generate stub and skeleton classes.
It is actually the underlying Implementation of Remote calls.
Finally, execute the Java helloworld console to print Hello, world, and call it successfully.