Home > Mobile >  ClassCastException occurs while trying to deserialize the serialized object
ClassCastException occurs while trying to deserialize the serialized object

Time:08-04

I am trying to deserialize the object in java and I am receiving this error [ClassCastException] constantly. The object is serialized but desrialization is not working. 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.

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.

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.

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.

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)

CodePudding user response:

Your 2 User classes are not the same, they are in different packages. The exception message is telling you this: serialization.serialize.User is a different class than serialization.deSerialize.User.

Using "default" binary serialization as you are, the class being deserialized into must be the exact same as the original object that was serialized. Even though your 2 User classes have the same structure, they are not compatible as far as serialization goes.

The simplest solution is to use the same User class for both serialization and deserialization. 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. The Jackson library makes this easy to do, and you can have different classes.

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.

  • Related