首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rmi远程异常。RMI不工作

rmi远程异常。RMI不工作
EN

Stack Overflow用户
提问于 2013-09-26 09:16:41
回答 2查看 7.2K关注 0票数 2

当我尝试运行我的RMI示例时,我得到了一个远程异常。我不明白原因何在。我运行程序时没有给程序本身或JVM任何参数。

请帮我摆脱这个异常。

非常感谢

这是我得到的异常:

代码语言:javascript
复制
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

以下是我拥有的类:

客户端类:

代码语言:javascript
复制
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();
    }
    }
}

远程接口:

代码语言:javascript
复制
package hello;

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

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

最后是服务器:

代码语言:javascript
复制
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();
    }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2013-09-26 16:32:19

注册表和/或客户端都找不到异常中指定的类。有几种可能的解决方案:

  1. 在执行注册表和客户端时将该类包含在类路径中。以及它所依赖的所有类,递归直至关闭。
  2. 使用LocateRegistry.createRegistry()从服务器JVM内部启动注册表,它解决了类路径问题,并仅在客户端的类路径上提供必要的类。
  3. 使用代码库功能来确保所有系统组件都可以访问所需的服务器端类。
票数 1
EN

Stack Overflow用户

发布于 2013-09-26 09:46:07

RMI trail中所述

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

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19017756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档