Deserializing the missing class. Java


Good day. I came across such an interesting case: There is a certain space (folder, MQ-queue, kafka topic, in general – it doesn't matter) where a Java-class object is written in serialized form. With the help of my listener, I listen to this space. And I pull out this object. Is it possible to deserialize or pull data from an object, provided that I do not have the class of this object? At least in the form of a map "someFieldName ==> someFieldValue".

Naturally, when trying to normal desrialization, I catch Class not Found. ObjectInputStream.getFiels throws NotActiveException.

Thanks in advance.


Try deserializing to Object . And then, using the reflection mechanism, pull out the class fields. For instance:

        P p = new P();

        try(ObjectOutputStream oos = new ObjectOutputStream(
                             new FileOutputStream(""))) {
        } catch(Exception ex) {

Written object P

        try(ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(""))) {

            Object someClass = ois.readObject().getClass().newInstance();
            Field[] fields = someClass.getClass().getDeclaredFields();
            System.out.println(fields[0].getGenericType() + " " +
                               fields[0].getName() + " = " +
        } catch(Exception ex) {

I get the output: int id = 0

Thus, you can get all the fields of a given (it is not clear which) class. Then you can do whatever you need with this array.

Scroll to Top