简体   繁体   English

使用readObject()接收对象时发生java.lang.ClassCastException

[英]java.lang.ClassCastException when receiving object with readObject()

I have a bean class on a Client side that stores user input data and send it through a socket to server. 我在客户端有一个bean类,用于存储用户输入数据并通过套接字将其发送到服务器。 Server has identical bean class. 服务器具有相同的Bean类。

Server receives object, but when I try to assign what was received to an instance of the bean class, it results in following error: 服务器接收对象,但是当我尝试将接收到的内容分配给Bean类的实例时,会导致以下错误:

java.lang.ClassCastException: ie.gmit.sw.client.methods.Client cannot be cast to ie.gmit.sw.server.methods.Client

Class with an error: 出现错误的类:

public class DriveableImpl implements Driveable{

    private Client client; 
    private ObjectOutputStream out; 
    private ObjectInputStream in; 

    public DriveableImpl() {
        client = new Client(); 
    }

    // Method that receives connection socket and open in/out streams for current session
    @Override
    public void connect(Socket socket){
        try {
            out = new ObjectOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean login() throws Exception {

        // Program crash here
        client = (Client) in.readObject();
        System.out.println(client.toString());

        return false;
    }
}

I use InvocationHandler to invoke methods implemented in class above: 我使用InvocationHandler来调用上述类中实现的方法:

public class MethodInvoker implements InvocationHandler{

    private Object returnObject = null; // object that will hold any returns from invoked methods
    private final Driveable userInterface; 


    protected MethodInvoker(Driveable ui) {
        this.userInterface = ui;
    }
    public Object invoke(Object proxy, Method method, Object[] args)
                  throws IllegalAccessException, IllegalArgumentException,
                  InvocationTargetException{

        System.out.println("BEFORE");
        returnObject = method.invoke(userInterface, args);
        System.out.println(method.getName());
        System.out.println("AFTER");

        return returnObject; // could problem be possibly here?
    }
}

I am really not sure what's going on here, as it works in simple design of a Client-Server app. 我真的不确定这里发生了什么,因为它可以在客户端-服务器应用程序的简单设计中工作。 I am posting, in my opinion, program parts that are relevant to an error, but I will modify post if any requests occur. 我认为我正在发布与错误相关的程序部分,但是如果发生任何请求,我将修改帖子。

Thank you! 谢谢!

Note the Serialization Error: 注意序列化错误:

It clearly states that you have 2 different classes (maybe they look exactly the same) but they are in different packages: 它明确指出您有2个不同的类(也许它们看起来完全一样),但是它们位于不同的包中:

ie.gmit.sw.**client**.methods
ie.gmit.sw.**server**.methods

This means, that from the point of view of Java they're completely unrelated 这意味着,从Java的角度来看,它们是完全无关的

ie.gmit.sw.client.methods.Client cannot be cast to ie.gmit.sw.server.methods.Client

That's the reason of why you have a class cast exception 这就是为什么您有一个类强制转换异常的原因

In Java the Same Class means that it resides in the same package and has the same name. 在Java中,Same Class表示它驻留在相同的程序包中并且具有相同的名称。 So you should have the same class in both server and client. 因此,服务器和客户端应具有相同的类。 There are two ways to achieve this: 有两种方法可以实现此目的:

  • Just put the same class twice - once in client and once in a server. 只需将相同的类放置两次-一次在客户端,一次在服务器。 In your case it means - rename the package to be something common. 对您而言,这意味着-将包重命名为通用名称。 This is a bad approach because of code duplication 由于代码重复,这是一种不好的方法

  • Place the objects that will pass the serialization into the third "common" module (jar will be produced out of it) and when you run the client and server make them dependent on this module. 将将序列化的对象放入第三个“ common”模块(将从中生成jar),然后在运行客户端和服务器时使它们依赖于此模块。 So physically you'll have only one copy of class "Client". 因此,从物理上讲,您只有一个“客户端”类的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM