{"record":{"id":"7010aca030091d11","repo":"apache/flink","slug":"error-during-java-serialization","errorCode":null,"errorMessage":"Error during Java serialization.","messagePattern":"Error during Java serialization\\.","errorType":"exception","errorClass":"KryoException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/kryo/JavaSerializer.java","lineNumber":63,"sourceCode":" */\npublic class JavaSerializer<T> extends Serializer<T> {\n\n    public JavaSerializer() {}\n\n    @SuppressWarnings({\"unchecked\", \"rawtypes\"})\n    @Override\n    public void write(Kryo kryo, Output output, T o) {\n        try {\n            ObjectMap graphContext = kryo.getGraphContext();\n            ObjectOutputStream objectStream = (ObjectOutputStream) graphContext.get(this);\n            if (objectStream == null) {\n                objectStream = new ObjectOutputStream(output);\n                graphContext.put(this, objectStream);\n            }\n            objectStream.writeObject(o);\n            objectStream.flush();\n        } catch (Exception ex) {\n            throw new KryoException(\"Error during Java serialization.\", ex);\n        }\n    }\n\n    @SuppressWarnings({\"unchecked\", \"rawtypes\"})\n    @Override\n    public T read(Kryo kryo, Input input, Class aClass) {\n        try {\n            ObjectMap graphContext = kryo.getGraphContext();\n            ObjectInputStream objectStream = (ObjectInputStream) graphContext.get(this);\n            if (objectStream == null) {\n                // make sure we use Kryo's classloader\n                objectStream =\n                        new InstantiationUtil.ClassLoaderObjectInputStream(\n                                input, kryo.getClassLoader());\n                graphContext.put(this, objectStream);\n            }\n            return (T) objectStream.readObject();\n        } catch (Exception ex) {","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/kryo/JavaSerializer.java#L45-L81","documentation":"JavaSerializer is a Kryo serializer shim that delegates to Java's native ObjectOutputStream. On write() it lazily creates an ObjectOutputStream wrapped over Kryo's Output (cached in the Kryo graph context) and calls writeObject(o). Any failure from Java serialization — most commonly the object graph containing a non-Serializable element, or a writeObject method throwing — surfaces as a KryoException saying 'Error during Java serialization.'.","triggerScenarios":"Kryo serializing an object with JavaSerializer (e.g. registered via Serializers.registerSerWithKryo or a type falling back to JavaSerializer) where the object or a nested field is not java.io.Serializable; a custom writeObject/ObjectOutputStream putFields mismatch; a field's type changing incompatibly between write and read; stream corruption from mixed serializers.","commonSituations":"Classes used in Flink data streams or in Kryo-serialized state that implement Serializable only partially (fields of non-serializable types like Thread, InputStream, or third-party objects without a Kryo serializer registered); upgrading a class and restoring old state; closures capturing non-serializable resources.","solutions":["Make the offending class and every referenced field implement java.io.Serializable (mark non-data fields transient).","Register a proper Kryo serializer for the problematic type (KryoSerializationSchema / env.registerTypeWithKryo or Serializers.registerSerializer) so JavaSerializer is not used.","Inspect the chained cause in the KryoException (NotSerializableException names the first non-serializable class) and fix that specific field.","For fields that cannot be serialized, restructure them as transient plus re-derivation on readObject, or use a custom Kryo Serializer that writes only the logical state."],"exampleFix":"// before\npublic class JobContext {\n    private Connection conn; // not serializable -> JavaSerializer fails\n}\n\n// after\npublic class JobContext implements java.io.Serializable {\n    private transient Connection conn;\n    private String connUrl;\n    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {\n        in.defaultReadObject();\n        conn = connect(connUrl); // re-derive on deserialize\n    }\n}","handlingStrategy":"validation","validationCode":"static void assertSerializable(Object sample) {\n    try (java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();\n         java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(bos)) {\n        oos.writeObject(sample);\n    } catch (java.io.IOException e) {\n        throw new IllegalStateException(\"Value fails Java serialization: \" + e.getMessage(), e);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    kryo.writeClassAndObject(output, record);\n} catch (com.esotericsoftware.kryo.KryoException e) {\n    Throwable cause = e.getCause();\n    if (cause instanceof java.io.NotSerializableException) {\n        // log which class is not serializable, fix model, fail job\n    }\n    throw e;\n}","preventionTips":["Unit-test round-trip serialization of your data types before deploying (serialize/deserialize a representative sample).","Register Kryo serializers for third-party types instead of letting them fall through to JavaSerializer.","Mark infrastructure fields (connections, threads, streams) transient."],"tags":["serialization","kryo","java-serialization","flink-core"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}