反序列化数据时发生 ClassNotFoundException
ClassNotFoundException while deserializing data
我正在序列化一个 Class 对象的映射和一个字符串列表,效果很好。但是,如果我尝试反序列化它,则会收到 ClassNotFoundException。
这是代码:
Map<Class<? extends IDomain>, List<String>> mapPresetImport = (Map<Class<? extends IDomain>, List<String>>) ois.readObject();
是什么导致了该异常?
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.
正如您在评论中提到的,序列化的 classes 在运行时 class 路径中不可用,因此导致 ClassNotFoundException
.
Is there another way to deserialize that
data without loading/looking for the class first?
不,这个依赖是必须的。在反序列化对象之前,您需要包含 classes(为了避免出现此类异常)。 Java 中使用的反序列化机制肯定会在它试图定位正在反序列化的 class(es) 的阶段失败。
我正在序列化一个 Class 对象的映射和一个字符串列表,效果很好。但是,如果我尝试反序列化它,则会收到 ClassNotFoundException。 这是代码:
Map<Class<? extends IDomain>, List<String>> mapPresetImport = (Map<Class<? extends IDomain>, List<String>>) ois.readObject();
是什么导致了该异常?
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.
正如您在评论中提到的,序列化的 classes 在运行时 class 路径中不可用,因此导致 ClassNotFoundException
.
Is there another way to deserialize that data without loading/looking for the class first?
不,这个依赖是必须的。在反序列化对象之前,您需要包含 classes(为了避免出现此类异常)。 Java 中使用的反序列化机制肯定会在它试图定位正在反序列化的 class(es) 的阶段失败。