Course contentsShow
← Java
Lesson 2129 of 2,87042. SerializationFree lesson

Using serialver Tool to Generate IDs

Learn to use the serialver command-line tool to generate serialVersionUID for existing classes.

Using serialver Tool to Generate IDs

What you'll learn: How to use Java's built-in serialver command-line tool to automatically generate a serialVersionUID for your serializable classes.

Why Use serialver?

You've learned that declaring an explicit serialVersionUID helps prevent InvalidClassException errors when deserializing objects. But what value should you use? While you could pick any long value (like 1L), Java provides the serialver tool to generate the exact same version ID that the JVM would compute automatically. This ensures consistency and helps document the "current" version of your class.

How serialver Works

The serialver tool is a command-line utility included with the JDK. It analyzes a compiled .class file and outputs the default serialVersionUID that the Java serialization mechanism would calculate for that class based on its structure (fields, methods, signatures, etc.).

Basic usage:

serialver com.example.MyClass

This outputs something like:

com.example.MyClass:    static final long serialVersionUID = -2545183682764864331L;

You can then copy this declaration directly into your class source code.

Using the -classpath option:

serialver -classpath /path/to/classes com.example.MyClass

This tells serialver where to find your compiled class files.

When to Use It

Use serialver when:

  • You're adding a serialVersionUID to an existing class for the first time
  • You want to match the default computed ID exactly
  • You're documenting the "baseline" version before making compatible changes

Key Takeaway: The serialver command-line tool generates the default serialVersionUID for a compiled class, giving you a consistent version identifier to declare explicitly in your source code.