Search code examples
javaserializationclassloaderclassnotfoundexceptionunsafe

how to deserialize an instance of "Serialization" without the implementation class on the classpath?


I'm writing a tool that need to get an instance of java.io.Serializable from a byte array.
The difficulty is that the "real" class is not (and cannot be...) on the classpath (I will not explain why here..).
The code below fails on is.readObject() with a ClassNotFoundException because the implementation class is not on the classpath
Q:
Is is possible to achieve this? by reflection? by using Unsafe? by using a sub class of ClassLoader? or...?

byte[] data = ...
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));) {
   Object o = ois.readObject();
   Serializable s = (Serializable)o;
}

Solution

  • Is iis possible to achieve this?

    No.

    by reflection?

    No.

    by using Unsafe?

    No.

    by using a sub class of ClassLoader?

    Yes but the class loader still has to get the class from somewhere. The RMI codebase feature is a good example.