简体   繁体   English

尝试反序列化序列化的 object 时发生 ClassCastException

[英]ClassCastException occurs while trying to deserialize the serialized object

I am trying to deserialize the object in java and I am receiving this error [ClassCastException] constantly.我正在尝试反序列化 java 中的 object 并且我不断收到此错误[ClassCastException] The object is serialized but desrialization is not working. object 已序列化,但反序列化不起作用。 I think there is no error in code anywhere but don't know why I am receiving this error.我认为任何地方的代码都没有错误,但不知道为什么我会收到此错误。 please help.请帮忙。

serialized object code: User class.序列化 object 代码:用户 class。

package serialization.serialize;

import java.io.Serializable;

public class User implements Serializable {
    String name;
    String password;
    public void hello(){
        System.out.println("Hello " + name);
    }
}

serialized object code: Main class.序列化 object 代码:主 class。

package serialization.serialize;

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        User user1 = new User();
        user1.name = "harry";
        user1.password = "123abc";

        FileOutputStream fileOut = new FileOutputStream("User1.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(user1);
        out.close();
        fileOut.close();
        System.out.println("Object info saved.");
    }
}

Now the Deserialization code.现在是反序列化代码。 User class.用户 class。

package serialization.deSerialize;

import java.io.Serializable;

public class User implements Serializable {
        String name;
        String password;
        public void hello(){
            System.out.println("Hello " + name);
        }
}

Main class.主营class。

package serialization.deSerialize;

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        User user1;
        FileInputStream fileIn = new FileInputStream("C:\\Users\\tgsra\\IdeaProjects\\javaBroCode\\src\\serialization\\serialize\\User1.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        user1 = (User)in.readObject();
        in.close();
        fileIn.close();
        System.out.println(user1.name);
        System.out.println(user1.password);
        user1.hello();
    }
}

This is the error i am getting.这是我得到的错误。

Exception in thread "main" java.lang.ClassCastException: class serialization.serialize.User cannot be cast to class serialization.deSerialize.User (serialization.serialize.User and serialization.deSerialize.User are in unnamed module of loader 'app')
    at serialization.deSerialize.Main.main(Main.java:10)

Your 2 User classes are not the same, they are in different packages.您的 2 User类不一样,它们位于不同的包中。 The exception message is telling you this: serialization.serialize.User is a different class than serialization.deSerialize.User .异常消息告诉您: serialization.serialize.Userserialization.deSerialize.User是不同的 class 。

Using "default" binary serialization as you are, the class being deserialized into must be the exact same as the original object that was serialized.使用“默认”二进制序列化,反序列化的 class 必须与序列化的原始 object 完全相同。 Even though your 2 User classes have the same structure, they are not compatible as far as serialization goes.即使您的 2 User类具有相同的结构,但就序列化而言,它们是不兼容的。

The simplest solution is to use the same User class for both serialization and deserialization.最简单的解决方案是使用相同的User class 进行序列化和反序列化。 If you must serialize one class and deserialize into a different class, I suggest you use a different form for the serialization, such as JSON.如果必须序列化一个 class 并反序列化成不同的 class,我建议你使用不同的序列化形式,例如 JSON。 The Jackson library makes this easy to do, and you can have different classes. Jackson 库使这很容易做到,并且您可以拥有不同的类。

It's also possible you could override writeObject() and readObject() methods to achieve this, but I'm not certain it will work with binary serialization.您也可以重写writeObject()readObject()方法来实现这一点,但我不确定它是否适用于二进制序列化。

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

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