【问题标题】:RMI on client side generating exception when getting a reference to the RMI server获取对 RMI 服务器的引用时,客户端上的 RMI 生成异常
【发布时间】:2010-06-07 15:26:17
【问题描述】:

当我在运行 RMI 服务器后尝试运行 RMI 客户端时,出现以下异常:

EncodeInterface exception: java.lang.ClassCastException: $Proxy30 cannot be cast to hw2.chat.backend.main.EncodeInterface
java.lang.ClassCastException: $Proxy30 cannot be cast to hw2.chat.backend.main.EncodeInterface
at hw2.chat.backend.main.EncodingRmiClient.initialiseRMIClient(EncodingRmiClient.java:26)

而RMI客户端中的相关代码是:

EncodeInterface encodeInterface;
Registry registry = LocateRegistry.getRegistry(host, portNumber);
encodeInterface = (EncodeInterface)registry.lookup("RmiEncodingServer"); //line 26

在 RMI 服务器中:

    try
    {
        EncodeInterface encodeInterface = new EncoderImpl();
        Registry registry = LocateRegistry.getRegistry();
        registry.rebind("RmiEncodingServer", encodeInterface);
        System.out.println("RmiEncodingServer is running...");
    }

EncodeInterface 是扩展Remote 的接口,它也存在于客户端。

Host 是“127.0.0.1”,portNumber 是 1099(我假设它应该是默认值 1099,因为我在运行 RmiEncodingServer 时没有指定它)。

如果我不运行 RMI 服务器,我会遇到同样的异常,你知道为什么会发生这种情况吗?

谢谢

【问题讨论】:

    标签: java rmi


    【解决方案1】:

    ClassCastException 通常意味着要么

    • EncoderImpl 没有实现 EncodeInterface,或者
    • 客户端和服务器端的.jar不同,导致要部署的类有两个版本,被认为是不同的。

    一些提示:

    1. 检查你是否实现了接口,例如

      EncoderImpl extends UnicastRemoteObject implements EncodeInterface

    2. 在 java serialization 支持的类中添加版本 ID,例如

      static final long serialVersionUID = 10275539472837495L;

    3. 清理、重建和重新部署,然后重新启动所有内容,以确保每次都部署相同的 jar,并且没有服务器在某处运行

    希望对您有所帮助,否则请在问题中提供更多信息。

    相关问题:Java RMI proxy issue

    【讨论】:

      【解决方案2】:

      问题是我在服务器端和客户端有不同的包名,但是分发给客户端的类需要与服务器端完全相同..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多