ComputeEngine的RMI教程错误

时间:2012-04-21 03:31:53

标签: java eclipse rmi

所以我花了很长时间试图找到其他人在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仍在运行,并且我没有关闭启动后出现的空窗口。

所以是的,我迷路了。

2 个答案:

答案 0 :(得分:1)

显然,您的安全策略文件未被找到。执行程序时,它需要位于当前工作目录中。使用-Djava.security.debug = access运行程序,无法确切了解正在发生的事情。

答案 1 :(得分:-1)

该例外清楚地表明您的代码库没有创建套接字/进行网络通信的权限。原因可能仅在于您的安全策略规范。请勿明确指定策略文件,并允许JVM使用默认安全策略。默认策略指定了正确的权限,因此您应该没问题。