Search code examples
javaobjectexceptiondeserializationclassnotfoundexception

ClassNotFoundException while deserializing data


I'm serializing a map of a Class object and a list of Strings, which works quite well. However if I try to deserialize it, I'm getting a ClassNotFoundException. Here's the code:

Map<Class<? extends IDomain>, List<String>> mapPresetImport = (Map<Class<? extends IDomain>, List<String>>) ois.readObject();

What could have caused that exception?


Solution

  • Ok, I got it now. IDomain is accessible, but the classes that are serialized are not, because they are in another package which are not included in the class path.

    As you mentioned in the comment that the classes which are serialized are not available in the runtime classpath, so that is resulting in the ClassNotFoundException.

    Is there another way to deserialize that data without loading/looking for the class first?

    No, this dependency is a must to have. You need to include the classes before deserializing the Objects (for getting no such exceptions). The deserialization mechanism used in Java will surely fail at the stage where it tries to locate the class(es) which is(are) being deserialized.