Naming.rebind(RMI_URL, remoteSearcher); System.out.println("RMI started at "+ RMI_URL); }catch(Exception e){ e.printStackTrace(); } } }
3.编写客户端代码,远程调用RMI对象 SearchClient.java
?[Copy to clipboard]View Code JAVAimport java.rmi.Naming; publicclass SearchClient { privatestatic final String RMI_URL ="rmi://172.16.102.6:10009/search"; public SearchClient(){ } publicstaticvoid main(String[] args){ try{ System.out.println(((RemoteSearcher) Naming.lookup(RMI_URL)).search("xiaoli")); }catch(Exception e){ e.printStackTrace(); } System.exit(0); } }
person.java
?[Copy to clipboard]View Code JAVApublic class Person implements Serializable { privatestatic final long serialVersionUID = 3069227031912694124L; privateString name; privateint age = 30; publicString getName(){ return name; } publicvoid setName(String name){ this.name = name; } public Person(String name,int age){ super(); this.name = name; this.age = age; } publicint getAge(){ return age; } publicvoid setAge(int age){ this.age = age; } @Override publicString toString(){ return"Person [age="+ age +", name="+ name +"]"; } }
4.启动服务端 ?[Copy to clipboard]View Code JAVAC:\Documents and Settings\Administrator>java -classpath D:\workspace\RMI\bin SearchServer RMI started at rmi://172.16.102.6:10009/search
5.执行客户端程序 ?[Copy to clipboard]View Code JAVAC:\Documents and Settings\Administrator>java -classpath D:\workspace\RMI\bin SearchClient Person [age=35, name=xiaoli]