Friday 26 August 2016

How to see the serialVersionUID of serialized class?

Java installation provides 'serialver' command line tool, to get the serialVersionUID of a serialized class.

Open command prompt (or) terminal, type 'serialver' command, you can get the usage help.

$ serialver
use: serialver [-classpath classpath] [-show] [classname...]


Employee.java

import java.io.Serializable;

public class Employee implements Serializable{
    int id;
    String firstName, lastName;
    
    Employee(int id, String firstName, String lastName){
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

}


$ javac Employee.java
$ 
$ serialver Employee
Employee:    private static final long serialVersionUID = 8478298351518747306L;

You can also specify serialVersionUID of a class by defining the variable serialVersionUID like below.

private static final long serialVersionUID = 1234L;


You may like





No comments:

Post a Comment