Can an application running in a version 1.1.x JVM read an object that was serialized by an application running in a version 1.2.x JVM?

Tim Rohaly

Changes were made to the default serialization stream format between version 1.1.x and version 1.2.x of the JDK. As a result, if you are writing objects with one version and reading them with another you may have to take special steps to ensure compatibility.

There are two distinct forms of the serialization protocol used. These protocol versions are identified by constants in the ObjectStreamConstants class:

  • ObjectStreamConstants.PROTOCOL_VERSION_1
  • ObjectStreamConstants.PROTOCOL_VERSION_2
The protocol version that is actually used during serialization is determined as follows:
  • JDK 1.1.x defaults to writing using PROTOCOL_VERSION_1
  • JDK 1.2.x defaults to writing using PROTOCOL_VERSION_2
  • JDK 1.1.7 and higher can read both protocol versions
Thus, if both applications use JDK 1.1.7 or higher, the serialization/deserialization will be successful.

Applications using JDK version 1.2 and higher may override the default serialization protocol used for writing objects by invoking the useProtocolVersion(int) method on their ObjectOutputStream, where the integer argument is one of the two constants shown above. Setting the protocol version to PROTOCOL_VERSION_1 ensures compatibility with all versions of Java when needed.