RMI client 'cannot cast to' exception

三世轮回 提交于 2019-12-13 03:37:04

问题


Let's say I have these lines:

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

The exception is this:

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

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


回答1:


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




回答2:


do not extends Remote if your class implements Serializable



来源:https://stackoverflow.com/questions/12496135/rmi-client-cannot-cast-to-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!