In this short Java tutorial, you will learn how to quickly generate a serialVersionUID for a given class. Most Java IDEs provide a support for generating serialVersionUID automatically while some require you to install a plugin for you to generate this value.
Generate SerialVersionUID for a Java Class
There is a built-in command in JDK you can use to generate a serialVersionUID for a selected class.
- Compile your project without the serialVersionUID first. This will generate Java classes in your project folder and you should be able to find them in the folder named classes,
- Open terminal window and change directory to the folder where your Java classes are located. NOTE: Change the directory into classes folder only and do not go further in. If your Java class is inside of a package, then DO NOT change your directory into the package. Stay in the root classes folder,
- In your terminal window run the following command:
serialver com.mypackage.MyJavaClassName
where:
– the serialver is the name of the built-in command to generate serialVersionUID for a class, and
– the com.mypackage.MyJavaClassName is a complete name of the class.Note: You must include the package name and should not include the .class extension.
- Once the serialver command is run successfully you should get a similar output:
private static final long serialVersionUID = 4865903039190150223L;
Check If Your IDE Already Supports SerialVersionUID
But the chances are that your IDE already provides support for generating a serialVersionUID value. To check that, make your class implement Serializable interface and then if your class name gets underlined, move your mouse over the class name and pause for 2-3 seconds. Most likely you will get a drop down box inviting you to generate the value. Using IDE is much more convenient than generating this value using the command line tool.
I hope this helps. If you enjoy learning Java by watching step-by-step video lessons have a look at the video courses below. One them might help you learn easier and faster.