开发者

RMI on client side generating exception when getting a reference to the RMI server

开发者 https://www.devze.com 2023-01-02 08:43 出处:网络
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.E

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 defaul开发者_运维知识库t 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


A ClassCastException usually mean that either

  • EncoderImpl does not implement EncodeInterface, or
  • The .jar on the client-side and server-side are different, resulting in two versions of the class to be deployed, which are considered different.

Few hints:

  1. Check that you implement the interface, e.g.

    EncoderImpl extends UnicastRemoteObject implements EncodeInterface

  2. Add a version id to your class as supported by java serialization, e.g.

    static final long serialVersionUID = 10275539472837495L;

  3. 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


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..

0

精彩评论

暂无评论...
验证码 换一张
取 消