// 第一步设计接口。
public interface Hello extends Remote {
public String hello() throws RemoteException;
}
// 第二步实现接口。
public class HelloImpl extends UnicastRemoteObject implements Hello {
public String hello() {
return "hello";
}
}
// 第三步开启服务。
public class Server {
public static void main(String[] argv) {
Hello hello = new HelloImpl();
Registry registry = LocateRegistry.getRegistry(8888);
registry.rebind("hello", hello);
}
}
// 第四步使用。
public class Client {
public static void main(String[] argv) {
Hello hello = Naming.lookup("rmi://127.0.0.1:8888/hello");
String s = hello.hello();
}
}
原文:http://blog.csdn.net/caipeichao2/article/details/38497363