package hello; public interface Hello extends java.rmi.Remote{ String sayHello() throws java.rmi.RemoteException; }
package hello; import java.rmi.*; public class HelloClient { public static void main(String args[]){ System.setSecurityManager(new RMISecurityManager()); try{ Hello obj = (Hello)Naming.lookup("HelloServer"); String message=obj.sayHello(); System.out.println(message); }catch(Exception e){ e.printStackTrace(); } } }
package hello; import java.rmi.*; import java.rmi.registry.LocateRegistry; import java.rmi.server.UnicastRemoteObject; public class HelloImpl extends UnicastRemoteObject implements Hello{ private String name; public HelloImpl(String s) throws java.rmi.RemoteException{ super(); name=s; } public String sayHello()throws RemoteException{ return "hello world"; } public static void main(String args[]){ System.setSecurityManager(new RMISecurityManager()); try{ HelloImpl obj = new HelloImpl("HelloServer"); LocateRegistry.createRegistry(1099); Naming.rebind("HelloServer", obj); System.out.println("HelloImpl created and bound in the registry to the name HelloServer"); }catch(Exception e){ e.printStackTrace(); } } }
安全策略文件
grant { permission java.security.AllPermission; }; 运行脚本:
Set CLASSPATH=%CLASSPATH%;c:\ 没有空格 javac -d .. *.java 在hello目录下 rmic -d . hello.HelloImpl 在hello的父目录下 java -Djava.security.policy=file:C:/java.policy hello.HelloImpl 注意安全策略文件 java -Djava.security.policy=file:/C:/java.policy hello.HelloClient 注意安全策略文件相关资料: http://blog.csdn.net/coolriver/archive/2004/09/10/100702.aspxhttp://topic.csdn.net/t/20020310/12/566253.htmlhttp://topic.csdn.net/u/20070426/08/b852e323-08c6-4f80-b87a-937e24af237d.html
|