RMI 客户端“无法转换为"异常 [英] RMI client 'cannot cast to' exception

查看:58
本文介绍了RMI 客户端“无法转换为"异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这些行:

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

例外是这样的:

java.lang.ClassCastException: $Proxy1 不能转换为 app.RemoteObject

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

For the sake of clarity, the RemoteObject implements an interface which extends java.rmi.Remote.

推荐答案

你需要转换到扩展 Remote 的接口

you need to cast to interface that extended Remote

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

这是因为你永远不会得到实际的对象,而是一个将任何方法调用转发到实际对象的存根对象

this is because you never get the actual object back but a stub object that forwards any method calls to the actual object

这篇关于RMI 客户端“无法转换为"异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆