1

次のコードでサンプルRMIアプリケーションを実行しようとしています


/* 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);
        }
    } 
}  

client.policy

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

server.policy

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