Reputation: 643
When I try to run an RMI client after running the RMI server I get the following exception:
EncodeInterface exception: java.lang.ClassCastException: $Proxy30 cannot be cast to hw2.chat.backend.main.EncodeInterface
java.lang.ClassCastException: $Proxy30 cannot be cast to hw2.chat.backend.main.EncodeInterface
at hw2.chat.backend.main.EncodingRmiClient.initialiseRMIClient(EncodingRmiClient.java:26)
And the relevant code in the RMI client is:
EncodeInterface encodeInterface;
Registry registry = LocateRegistry.getRegistry(host, portNumber);
encodeInterface = (EncodeInterface)registry.lookup("RmiEncodingServer"); //line 26
And in the RMI server:
try
{
EncodeInterface encodeInterface = new EncoderImpl();
Registry registry = LocateRegistry.getRegistry();
registry.rebind("RmiEncodingServer", encodeInterface);
System.out.println("RmiEncodingServer is running...");
}
EncodeInterface
is the interface that extends Remote
and that is present in the client side too.
Host is "127.0.0.1" and portNumber is 1099 (I am assuming it should be the default value which is 1099 since I didn't specify it when I ran my RmiEncodingServer
).
If I don’t run the RMI server I get the same exception, any ideas as to why this is happening?
thanks
Upvotes: 1
Views: 1237
Reputation: 643
The problem was that I had a different package name on the server and client side, but the classes distributed to the client need to be exactly the same as in the server..
Upvotes: 1
Reputation: 38605
A ClassCastException
usually mean that either
EncoderImpl
does not implement EncodeInterface
, orFew hints:
Check that you implement the interface, e.g.
EncoderImpl extends UnicastRemoteObject implements EncodeInterface
Add a version id to your class as supported by java serialization, e.g.
static final long serialVersionUID = 10275539472837495L;
Clean, rebuild, and redeploy, and restart everything to make sure the same jar are deployed everywere and that there isn't a server running somewhere already
Hope it helps, otherwise provide more info in the question.
Related question: Java RMI proxy issue
Upvotes: 1