Java serialver command examples
- Details
- Written by Nam Ha Minh
- 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:
serialver [-classpath classpath] [-show] [classname...]
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:
serialver -classpath build\classes net.codejava.mail.SwingEmailSender
Output:
net.codejava.mail.SwingEmailSender: static final long serialVersionUID = -2279575288884790698L;
- Displaying serialVersionUID numbers of two classes:
Command:
serialver CustomPanel TestSwingZoom
Output:
CustomPanel: static final long serialVersionUID = 6858515184837974475L; TestSwingZoom: static final long serialVersionUID = 4136458911881435009L;
- Displaying serialVersionUID of a class inside a jar file:
Command:
serialver -classpath EmailSender.jar net.codejava.swing.JFilePicker
Output:
net.codejava.swing.JFilePicker: static final long serialVersionUID = -6241197264668893626L;
- Displaying serialVersionUID of two classes inside two jar files:
Command:
serialver -classpath SwingAudio.jar;SwingUpload.jar net.codejava.AudioPlayer net.codejava.FileUpload
Output:
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:
References:
Java Serialization Tutorials:
- Why Do We Need Serialization in Java?
- Java Serialization Basic Example
- Understanding Java Externalization with Examples
Other Java Tools Tutorials:
- javac command examples
- java command examples
- Java jar command examples
- Understanding the triad tools javac, java and jar in JDK
- The Java Shell (jshell) Tutorial
- How to compile, package and run a Java program using command-line tools (javac, jar and java)
Comments
-show
is no more valid in latest JDK (e.g. JDK11)