为什么同一个类的序列化对象在两个不同的项目中是不同的

时间:2014-01-03 09:50:22

标签: java swing servlets

所以我有两个项目。其中一个是servlet,另一个是JFrame。我试图在servlet中读取序列化对象,但我得到了ClassNotFoundException。只有当我把所有类编译并在我的servlet项目中编译它时,它才开始工作。这是应该的方式吗? serialization是否取决于它编译的项目?(如果课程类型相同)

UPD 我的类的定义,例如,图在servletJFrame类中。

2 个答案:

答案 0 :(得分:2)

来自javadocs

When an object is serialized, information that identifies its class is recorded in
the serialized stream. However, the class's definition ("class file") itself is
not recorded. It is the responsibility of the system that is deserializing the
object to determine how to locate and load the necessary class files.

答案 1 :(得分:1)

反序列化将要求.class文件从序列化流创建对象。由于序列化对象不包含类定义,因此无法在没有.class文件的情况下从流中提取对象。