1

我正在尝试使用以下代码执行示例 RMI APPLICATION


/* DemoInterface.java*/ //STEP-1  
//Create the remote interface  
import java.rmi.Remote;   
import java.rmi.RemoteException;   
public interface DemoInterface extends Remote  
{  
    public String SayDemo() throws RemoteException;  

}    

    import java.rmi.Naming;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    public class DemoClient 
    {
        public static void main (String[] args)
        {
            if (System.getSecurityManager() == null)
            {
                System.setSecurityManager(new SecurityManager());
            }
            try
            {
                Registry reg = LocateRegistry.getRegistry(args[0]);      
                DemoInterface h = (DemoInterface) reg.lookup("f1");
                System.out.println (h.SayDemo());
            }
            catch (Exception e)
            {
                System.out.println ("DemoClient exception: " + e);
            }
        }
    }  


----------

import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.RemoteException;
public class DemoServer implements DemoInterface
{
    private String message;
    public DemoServer() 
    {
           super();
    }
    public DemoServer(String msg) throws RemoteException
    {
        message = msg;
    }
    public String SayDemo() throws RemoteException
    {
        return message;
    }
    public static void main (String[] argv) 
    {
        try
        {   DemoInterface h = new DemoServer("Advance jAVA");
            DemoInterface stub = (DemoInterface)   UnicastRemoteObject.exportObject(h, 0);
            Registry reg = LocateRegistry.getRegistry();
            reg.rebind ("f1", stub);
            //System.out.println ("Server is connected and ready for  operation.");
        } 
        catch (Exception e)
        {
          System.out.println("Server not connected: " + e);
        }
    } 
}  

客户政策

grant codeBase "file:/RMI/DemoWorld/"
{
    permission javsecurity.AllPermission;
};  

服务器策略

grant codeBase "file:/RMI/DemoWorld/"
{
    permission javsecurity.AllPermission;
};

要运行应用程序,我遵循以下给定步骤

  1. -启动 rmiregistry
  2. java -cp D:\RMI\DemoWorld; Djava.rmi.server.codebase=file:/D/RMI/DemoWorld/ -Djava.rmi.server.hostname=127.0.0.1 -Djava.security.policy=server.policy DemoServer

  3. java -cp D:\RMI\DemoWorld; -Djava.rmi.server.codebase=file:/RMI/DemoWorld/ -Djava.security.policy=client.policy DemoClient 127.0.0.1
    但我收到以下错误

DemoClient 异常:java.security.AccessControlException:访问被拒绝(“java .net.SocketPermission”“127.0.0.1:1099”“connect,resolve”)

4

0 回答 0