3

所以我花了很长时间试图从其他人那里找到答案,这些人对 RMI 教程有问题,但我完全被这个难住了。我正在通过 Eclipse 完成本教程。

我的 ComputeEngine 类。这只是从教程中复制的,所以我认为它没有任何问题。

import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import compute.Compute;
import compute.Task;

public class ComputeEngine implements Compute {

    public ComputeEngine() {
        super();
    }

    public <T> T executeTask(Task<T> t) {
        return t.execute();
    }

    public static void main(String[] args) {
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new RMISecurityManager());
        }
        try {
            String name = "Compute";
            Compute engine = new ComputeEngine();
            Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
            Registry registry = LocateRegistry.getRegistry();
            registry.rebind(name, stub);
            System.out.println("ComputeEngine bound");
        } catch (Exception e) {
            System.err.println("ComputeEngine exception:");
            e.printStackTrace();
        }
    }
}

我在命令行中启动 rmiregistry

set classpath=
start rmiregistry

我在 Eclipse 中的 VM 参数是:

-Djava.rmi.server.codebase=file:/C:/Users/Kevin/workspace/RMI/bin/
-Djava.rmi.server.hostname=Compute
-Djava.security.policy=server.policy

我在 bin 文件夹中有 compute.jar 文件和 server.policy 文件。我授予了策略文件的所有权限。

grant{
    permission java.security.AllPermission;
};

毕竟,我运行 ComputeEngine 并收到以下错误:

ComputeEngine exception:
java.security.AccessControlException: access denied (java.net.SocketPermission          127.0.0.1:1099 connect,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at engine.ComputeEngine.main(ComputeEngine.java:31)

重新绑定似乎有某种问题,但我不明白是什么。当我有策略文件时,我也不理解 AccessControlException。我已经检查以确保 rmiregistry 仍在运行,并且我没有关闭启动后出现的空窗口。

所以,是的,我迷路了。

4

2 回答 2

1

显然您的安全策略文件没有被发现。执行程序时,它需要位于当前工作目录中。使用 -Djava.security.debug=access,failure 运行您的程序以查看到底发生了什么。

于 2012-04-21T08:48:56.997 回答
-1

The exception clearly says that your code base does not have permission to creat sockets/ for network communication. The reason could be with your security policy specification alone. Do not specify the policy file explicitly and allow the JVM to use the default security policy. The default policy specifies the right permissions so you should be fine.

于 2012-04-21T06:00:12.053 回答