【问题标题】:RMI client 'cannot cast to' exceptionRMI 客户端“无法转换为”异常
【发布时间】:2012-09-19 13:48:20
【问题描述】:

假设我有这些行:

Registry registry = LocateRegistry.getRegistry(2121);
RemoteObject probe = (RemoteObject)registry.lookup(REMOTE_OBJ_NAME);//this throws exception
probe.doSomething();

例外是这样的:

java.lang.ClassCastException: $Proxy1 cannot be cast to app.RemoteObject

为了清楚起见,RemoteObject 实现了一个扩展 java.rmi.Remote 的接口。

【问题讨论】:

标签: java rmi


【解决方案1】:

您需要转换为扩展 Remote 的接口

RemoteInterface probe = (RemoteInterface)registry.lookup(REMOTE_OBJ_NAME);
probe.doSomething(); 

这是因为您永远不会取回实际对象,而是将任何方法调用转发给实际对象的存根对象

【讨论】:

  • 有人发表了同样建议的评论。问题解决了!
【解决方案2】:

如果你的类实现了 Serializable,则不要扩展 Remote

【讨论】:

  • (a) 为什么不呢? (b) 没有回答问题。
猜你喜欢
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多