【问题标题】:rmi remote exception. RMI doesnt workrmi 远程异常。 RMI 不工作
【发布时间】:2013-09-26 01:16:41
【问题描述】:

当我尝试运行我的 RMI 示例时出现远程异常。我不明白为什么。 我运行程序时不向程序本身或 JVM 提供任何参数。

请帮助我摆脱异常。

非常感谢

这是我得到的例外:

Server exception: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: hello.Hello
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: hello.Hello

这些是我的课程:

客户端类:

package hello;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {

    private Client() {}

    public static void main(String[] args) {

    String host = (args.length < 1) ? null : args[0];
    try {
        Registry registry = LocateRegistry.getRegistry(host);
        Hello stub = (Hello) registry.lookup("Hello");
        String response = stub.sayHello();
        System.out.println("response: " + response);
    } catch (Exception e) {
        System.err.println("Client exception: " + e.toString());
        e.printStackTrace();
    }
    }
}

远程交互:

package hello;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Hello extends Remote {
    String sayHello() throws RemoteException;
}

最后是服务器:

package hello;

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server implements Hello {

    public Server() {}

    public String sayHello() {
    return "Hello, world!";
    }

    public static void main(String args[]) {

    try {
        Server obj = new Server();
        Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

        // Bind the remote object's stub in the registry
        Registry registry = LocateRegistry.getRegistry("localhost");
        registry.bind("Hello", stub);

        System.err.println("Server ready");
    } catch (Exception e) {
        System.err.println("Server exception: " + e.toString());
        e.printStackTrace();
    }
    }
}

【问题讨论】:

  • 很可能是客户端找不到接口。客户端和服务器都需要有接口的副本,在 Java 6 之前,您还需要使用 rmic 来构建客户端存根
  • 您是否尝试过使用Spring-Remoting。这将使 RMI 变得非常容易。
  • 当你没有发送参数时,你正在做getRegistry(null)。另外,您使用的是哪个操作系统?如果您在 Ubuntu 中,127.0.1.1:1099 是主机地址。
  • @EJP 感谢您清除它。当我开始使用 RMI 时,我一直坚持使用 127.0.0.1。 ://

标签: java eclipse rmi


【解决方案1】:

注册表或客户端或两者都找不到异常中命名的类。有几种可能的解决方案:

  1. 在执行注册表和客户端时将该类包含在类路径中。以及它所依赖的所有类,递归直到关闭。

  2. 从服务器 JVM 内部启动 Registry,使用 LocateRegistry.createRegistry(),这解决了类路径问题,并仅在客户端的类路径上提供必要的类。

  3. 使用代码库功能确保所有系统组件都可以访问所需的服务器端类。

【讨论】:

    【解决方案2】:

    RMI trail中所述

    您可能需要提供java.rmi.server.codebase 参数,该参数列出了 RMI 服务器导出对象所需的所有 jar...

    请参阅Running the examples 并查看“启动服务器”部分。另请务必查看有关运行客户端程序的部分以获取其他建议参数

    【讨论】:

    • 这只是可能的解决方案之一。他根本“不需要”这样做。
    • @EJP 真的,因为这是我让它工作的唯一方法
    • 确实如此。这不是它可以工作的唯一方式。看我的回答。
    • Solution 1 在测试时似乎对我不起作用。类路径是否与 RMIRegistry 本身相关?
    • @EJP 如果您尝试通过单个注册表部署多个解决方案,这似乎是“首选”解决方案,但话又说回来,您可能会考虑使用多个注册表......只是大声思考; ) - 正如你所说,这是“A”解决方案;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 2010-10-11
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    相关资源
    最近更新 更多