1、定义远程接口
2、实现远程接口
3、编写客户端
4、编写权限文件
RMIpolicy.policy
grant
{
permission java.security.AllPermission;
};
5、生成Sub类和Skeleton类(jdk1.5后默认只生成sub)、编译相关程序
生成Sub类和Skeleton类:rmic UpperImpl
6、运行程序
(1) 启动RMI服务器:
start rmiregistry
(2)启动服务器端程序
start java -Djava.security.policy=RMIpolicy.policy host/UpperImpl
(3)运行客户机端程序:
java UpperApp
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Upper extends Remote{
public String toUpper(String str) throws RemoteException;
}
2、实现远程接口
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.*;
public class UpperImpl extends UnicastRemoteObject implements Upper{
public UpperImpl() throws RemoteException{ super(); };
public String toUpper(String str) throws RemoteException{
return str.toUpperCase();
}
public static void main(String[] args){
System.setSecurityManager(new RMISecurityManager());
try{
Upper up=new UpperImpl();
Naming.rebind("rmi://127.0.0.1:1099/UpperServer",up);
}
catch(Exception e){
e.printStackTrace();
}
}
}
3、编写客户端
import java.rmi.*;
import java.net.*;
public class UpperApp{
public static void main(String a[]){
String res=null;
//System.setSecurityManager(new RMISecurityManager());
try{
Upper up=(Upper)Naming.lookup("rmi://127.0.0.1/UpperServer");
res=up.toUpper("fuck you !");
}catch(Exception e){
e.printStackTrace();
}
System.out.println(res);
}
}
4、编写权限文件
RMIpolicy.policy
grant
{
permission java.security.AllPermission;
};
5、生成Sub类和Skeleton类(jdk1.5后默认只生成sub)、编译相关程序
生成Sub类和Skeleton类:rmic UpperImpl
6、运行程序
(1) 启动RMI服务器:
start rmiregistry
(2)启动服务器端程序
start java -Djava.security.policy=RMIpolicy.policy host/UpperImpl
(3)运行客户机端程序:
java UpperApp