import java.rmi.*;//服务器
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote
{
public String sayHello()
{
return "Server says,'Hey'";
}
public MyRemoteImpl()throws RemoteException{}
public static void main(String [] args)
{
try{
MyRemoteImpl service=new MyRemoteImpl();
Naming.rebind("Remote Hello",service);
}catch(Exception ex){ex.printStackTrace();}
}
}
import java.rmi.*;
public interface MyRemote extends Remote
{
public String sayHello()throws RemoteException;
}
import java.rmi.*;//客户端
public class MyRemoteClient
{
public static void main(String [] args)
{
new MyRemoteClient().go();
}
public void go()
{
try{
Remote service=(Remote)Naming.lookup("rmi://127.0.0.1/Remote Hello");
String s=service.sayHello();
System.out.println(s);
}catch(Exception ex){ex.printStackTrace();}
}
}