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();
}
}
}
安全策略文件
運行腳本:
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;
};
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.aspx
http://topic.csdn.net/t/20020310/12/566253.html
http://topic.csdn.net/u/20070426/08/b852e323-08c6-4f80-b87a-937e24af237d.html