serialver
Return class serialVersionUID
TLDR
Display the serialVersionUID of a class
Display the serialVersionUID for a colon-separated list of classes and resources
Use a specific option from reference page of Java application launcher to the Java Virtual Machine
SYNOPSIS
serialver [ options ] classnames
classnames: One or more fully qualified Java class names (e.g., java.lang.String
).
PARAMETERS
-classpath path
or -cp path
Specifies the path where serialver should look for class files. This path can contain directories and JAR archives, separated by the platform-specific path separator (e.g., :
on Linux, ;
on Windows).-show
Displays a simple graphical user interface (GUI) that allows the user to enter a fully qualified class name and view its serialVersionUID. This option might behave differently or be omitted in headless environments or recent JDK versions.-Jflag
Passes the specified flag directly to the underlying Java Virtual Machine (JVM) that runs serialver. For example, -J-Xmx512m
would set the maximum heap size for the JVM.
DESCRIPTION
serialver is a command-line utility provided with the Java Development Kit (JDK) that returns the serialVersionUID for one or more Java classes. This ID is crucial for Java's object serialization mechanism. When a Java object is serialized (converted into a byte stream for storage or transmission), it carries this unique identifier. Upon deserialization (reconstruction of the object from the byte stream), the serialVersionUID of the serialized object is compared with the serialVersionUID of the class definition currently available in the Java Virtual Machine (JVM). If these IDs do not match, an InvalidClassException is thrown, indicating a version mismatch.
Developers use serialver to determine the default serialVersionUID generated by the Java compiler. This allows them to explicitly declare this ID in their class definition (e.g., private static final long serialVersionUID = 12345L;
) to ensure backward and forward compatibility for serialized objects, especially when class definitions might evolve slightly without breaking serialization compatibility.
CAVEATS
serialver is a Java utility and requires a Java Development Kit (JDK) installation to be available in the system's PATH. It is primarily used for Java class files and is not a general-purpose Linux command. The -show
GUI option may not function as expected in headless server environments or may be simplified to a modal dialog in newer JDK versions. Its output is specifically for Java's object serialization mechanism.
IMPORTANCE OF <I>SERIALVERSIONUID</I>
The serialVersionUID is a version control mechanism for a serialized class. If a class definition changes in a way that is incompatible with previous versions (e.g., changing non-static, non-transient fields), the serialVersionUID should ideally be updated. If serialVersionUID is not explicitly declared in a Serializable class, the Java compiler automatically generates one based on the class's structure. This auto-generated ID can change even for minor, source-compatible changes (like adding a comment or changing method implementation), which can lead to InvalidClassException when deserializing older objects. Explicitly declaring serialVersionUID (e.g., using the ID provided by serialver) ensures that old serialized objects can still be deserialized successfully by a new version of the class, as long as the changes are compatible (e.g., adding new fields or methods without changing existing ones).
HISTORY
The serialver utility has been an integral part of the Java Development Kit (JDK) since its early versions, specifically introduced around JDK 1.2. Its purpose emerged with the increasing importance of Java's object serialization API for persistent storage and inter-process communication of Java objects. As Java applications evolved and required version compatibility for serialized data, serialver provided developers with a standardized way to inspect and manage serialVersionUIDs, which became critical for maintaining data integrity across different versions of a class.