javalinuxserialversionuid

How do I generate the serial version UID of a Java file from command line?


Background

I am attempting to generate the serialVersionUID of my java files in eclipse but get the error described in Eclipse : Cannot generate a serial version ID. I have followed the directions in that question, but I have no missing path files and still get the same error. In addition, I must use a randomly generated serialVersionUID (even though it is generally ok to use just 1L) because it is a requirement of my project.

Problem

Since my eclipse is being uncooperative, I am trying a command line solution. The command I found, serialver, does not generate a serialVersionUID. According to How to generate SerialVersionUID serialver should generate a serialVersionUID within the file:

  1. serialver command

JDK has a build in command called “serialver” to generate the serialVersionUID automatically. In this example, you use “serialver” to generate a serialVersionUID for Address class.

 E:\workspace\target\classes>serialver Address Address:    static final
    long serialVersionUID = -687991492884005033L;

However, I ran this command on one of my classes inside the class' directory and the terminal returned this error:

Class Classname.java not found.

This error was given regardless if I typed in serialver Classname or serialver Classname.java into the terminal. I also ran it in the same directory as the files and the error persisted.

Question

How do I use serialver in Linux command line, to set the serialVersionUID of a Java file? If serialver is not the command I should be using, which command (or series of commands) is the proper method?

Clarification

I want to have a generated and assigned serialVerionUID in my file from the command line, therefore the programmatic approach of How to generate serialVersionUID programmatically in Java? is not what I am looking for. In other words, I want the command to generate this bit of code at the top of my file:

private static final long serialVersionUID = <some random long>;

Solution

  • You may need to specify -classpath for the directory that contains your compiled classes (e.g. bin). To refer to a class you must use its fully qualified name, including the package -- e.g. for an arraylist class

    serialver -classpath bin java.util.ArrayList