Last Updated on 04 August 2019   |   Print Email
The serialver is a tool that comes with JDK. It is used to get serialVersionUID numbers of one or more Java classes. Basically, a serializable class must declare a constant named serialVersionUID (either explicitly by the programmer or implicitly by the compiler) like this:
public class User implements Serializable {
private static final long serialVersionUID = 1234L;
}
The serialVersionUID number is important when dealing with the serialization/de-serialization of objects of a class. The serialver command is helpful in case we want to know that number without having source code of the class. Its syntax is as simple as follows:
The -classpath option specifies where to look for the classes (in directories or jar files, separated by semicolon marks ;). The -show option displays a simple user interface that allows the user to enter a full class name and then press Enter key or click Show button to display the serialVersionUID number.Now, let’s see some examples.
Displaying serialVersionUID of a class in default package:
Command:
serialver ProgressBarDemo
Output:
ProgressBarDemo: static final long serialVersionUID = 8449967402779341329L;
If the specified class is not serializable, then this message is displayed:
Class ProgressBarDemo is not Serializable.
Displaying serialVersionUID of a class in a specific package:
The following command displays the serialVersionUID of the class SwingEmailSender under the package net.codejava.mail, assuming that the classes are in the build\classes directory:
net.codejava.AudioPlayer: static final long serialVersionUID = 5499535881565379674L;
net.codejava.FileUpload: static final long serialVersionUID = 6409440265121275085L;
Showing the user interface:
Command:
serialver -classpath EmailSender.jar -show
The Serial Version Inspector window gets displayed, enter the full class name and click the Show button to display the serial version:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments
-show
is no more valid in latest JDK (e.g. JDK11)