This article shows you a few ways to generate the serialVersionUID for serialization class.

1. serialver

JDK has a built-in command serialver to generate a serialVersionUID automatically.

In this example, we use serialver to generate a serialVersionUID for an Address class.

Terminal

$ serialver Address

Address:    static final long serialVersionUID = -687991492884005033L; 

2. Eclispe IDE

For Eclipse IDE, move the mouse over the serialization class, or click on the serialization class and press CTRL + 1.

3. Intellij IDEA

Unlinke Eclipse IDE, we need to enable the auto generate serialVersionUID option manually.

In IntelliJ IDEA, we need to enable this auto-generate serialVersionUID option manually.

P.S Tested with IntelliJ IDEA 2019.3.4, it should work the same in other versions.

Intellij IDEA Settings

File -> Settings -> Editor -> Inspections -> Java -> Serialization issues:

Find serialization class without serialVersionUID and check it.

Back to the editor, clicks on the class name, ALT + ENTER (Windows), it will prompts the Add serialVersionUID field option.

A new serialVersionUID is auto-generated.

public class Address implements Serializable {

    private static final long serialVersionUID = -2338626292552177485L;

}

4. 1L

Puts serialVersionUID=1L; It should be sufficient in most cases.

private static final long serialVersionUID = 1L;

Further Reading
What is serialVersionUID

References

Leave a Reply

Your email address will not be published. Required fields are marked *