Java uses ObjectOutputStream to serialize objects across projects

Let me ask you a question:
A project uses ObjectOutputStream to serialize ClassA objects to save binaries. The
B project deserializes this file, but ClassA is not in the same directory as the A project. Causes a Caused by: java.lang.ClassNotFoundException exception.
is there any other solution besides making it into the same directory?

Apr.19,2022

serialization is just one way, and it's OK if you want to continue using JAVA native serialization, but you need to encapsulate an API.jar so that two distributed projects depend on it separately. This solves the problem you have now.
in addition, the specific ways of serialization also need to carefully consider the performance of different serialization methods.


is there an error in serialization or deserialization?


then do not use ObjectOutputStream serialization, the java serialization mechanism serializes the object's class name and version number into the byte stream so that the deserialization side recognizes the class and constructs the class's object. You don't have class An at all in project B, so instead of using the java serialization mechanism, use other serializations, such as serializing to Json strings.

Menu