java - ComputeEngine 的 RMI 教程错误

标签 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)

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

是的,我迷路了。

最佳答案

很明显,找不到您的安全策略文件。当您执行该程序时,它需要位于当前工作目录中。使用 -Djava.security.debug=access,failure 运行你的程序,看看到底发生了什么。

关于java - ComputeEngine 的 RMI 教程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10255949/

相关文章:

通过互联网的 Java RMI 连接

java - Java连接MySql中的PasswordFiled char[]到String?

java - 在 MongoDB 中定义和检索键值

java - Eclipse java 项目 forName() classnotfoundException

eclipse - 为什么 "Install New Software..."菜单项位于 eclipse 中的“帮助”下

java - 基于非容器的java远程处理?

security - 使用 t3s weblogic 的 SSLHandshakeException

java - 打开远程文件并写入

java - hibernate HQL 日期差异

java - Eclipse 进入调试