java runtime_error ai_generated true

java.io.NotSerializableException: com.example.MyClass

ID: java/not-serializable-exception

Also available as: JSON · Markdown · 中文
92%Fix Rate
87%Confidence
1Evidence
2024-01-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Java 8 active
Java 11 active
Java 17 active
Java 21 active

Root Cause

An object of a class that does not implement java.io.Serializable is being serialized, typically during object output, RMI, or session replication.

generic

中文

尝试序列化未实现 java.io.Serializable 接口的类的对象,通常发生在对象输出、RMI 或会话复制过程中。

Official Documentation

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/NotSerializableException.html

Workarounds

  1. 95% success Implement java.io.Serializable in the class: public class MyClass implements Serializable { private static final long serialVersionUID = 1L; }
    Implement java.io.Serializable in the class: public class MyClass implements Serializable { private static final long serialVersionUID = 1L; }
  2. 75% success If the class cannot be modified, use a custom serialization mechanism like Jackson or writeExternal for Externalizable.
    If the class cannot be modified, use a custom serialization mechanism like Jackson or writeExternal for Externalizable.

中文步骤

  1. Implement java.io.Serializable in the class: public class MyClass implements Serializable { private static final long serialVersionUID = 1L; }
  2. If the class cannot be modified, use a custom serialization mechanism like Jackson or writeExternal for Externalizable.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The serialVersionUID field is only relevant for Serializable classes; without implementing the interface, the exception persists.

  2. 80% fail

    Transient fields prevent serialization of those fields, but the class itself must still implement Serializable to be serialized.