问题
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