1

我在创建 RMIserver 类时遇到问题,因为我不断收到此错误:访问被拒绝(java.net.SocketPermission 127.0.0.1:1099 connect,resolve)

现在我在谷歌上读到一些关于创建新政策文件的内容,但我真的不明白该怎么做,请有人帮助我吗?

这是我的服务器代码:

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*; 
import java.security.Permission;
import java.security.Security;

public class RmiServer extends UnicastRemoteObject 
    implements RmiServerIntf 
    {
    public static final String MESSAGE = "Hello world";

    public RmiServer() throws RemoteException
    {
    }

    public String getMessage()
    {
        return MESSAGE;
    }


    public static void main(String args[])
    {
        System.out.println("RMI server started");

        // Create and install a security manager
        if (System.getSecurityManager() == null)
        {
            System.setSecurityManager(new RMISecurityManager());
            System.out.println("Security manager installed.");
        }
        else
        {
            System.out.println("Security manager already exists.");
        }

        try
        { //special exception handler for registry creation
            LocateRegistry.createRegistry(1099); 
            System.out.println("java RMI registry created.");
        } 
        catch (RemoteException e)
        {
            //do nothing, error means registry already exists
            System.out.println("java RMI registry already exists.");
        }

        try 
        {
            //Instantiate RmiServer
            RmiServer obj = new RmiServer();


            // Bind this object instance to the name "RmiServer"
            Naming.rebind("//localhost/RmiServer", obj);

            System.out.println("PeerServer bound in registry");
        } 
        catch (Exception e) 
        {
            System.err.println("RMI server exception:" + e);
            e.printStackTrace();
        }
    }

}    
4

2 回答 2

2

您正在使用 SecurityManager(为什么?),但您的安全策略未授予您在异常中指定的权限。除非您打算使用 RMI 代码库功能,否则您根本不需要 SecurityManager:是吗?

于 2012-05-12T16:55:46.487 回答
0

确实需要阅读 RMI 的手册,以及如何创建策略文件:

http://docs.oracle.com/javase/tutorial/rmi/running.html

于 2012-05-12T20:56:14.543 回答