【发布时间】:2015-04-22 10:20:54
【问题描述】:
我正在开发 RMI 聊天系统,但在运行客户端时遇到异常。 我得到的例外是:
Server failed: java.lang.ClassCastException: com.sun.proxy.$Proxy1 cannot be cast to rmi.ClientInterface
发生异常的行在我的主类中,如下:
ClientInterface server =(ClientInterface) Naming.lookup("rmi://localhost/ABC");
其中 ClientInterface 是一个扩展 Remote 的接口。 知道如何解决这个问题吗? 谢谢
【问题讨论】:
-
你确定你的服务器
ìmplements ClientInterface。不应该是ServerInterface之类的吗? -
也可以试试 PortableRemoteObject.narrow(Object narrowFrom, ClassnarrowTo) 而不是强制转换。
-
@regde 只有当它是一个 RMI/IIOP 远程对象时它才有效,但它不是。而且你仍然需要转换
narrow()的结果。