I've got a class which defines public static final Long serialVersionUID = 123L;
.
When I actually serialize it or even if I run it through Java's serialver.exe
it comes back with an arbitrary auto-generated serialVersionUID dependent on the method signatures at compilation time or however Java normally derives those.
Why is Java ignoring my explicitly specified serialVersionUID
and how can I get it to stop?
Edit: Here's a minimal example which also demonstrates the above behavior.
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demonstration</groupId>
<artifactId>serialization</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
src/main/java/demonstration/serialization/Example.java:
package demonstration.serialization;
import java.io.Serializable;
public class Example implements Serializable {
private static final Long serialVersionUID = 123L;
}
Do an mvn clean package
to create the output jar, then run the following command (modified as appropriate for your JDK):
"C:\Program Files\AdoptOpenJDK\jdk-14.0.1.7-openj9\bin\serialver.exe" -classpath target/serialization-1.0.jar demonstration.serialization.Example
I get back:
demonstration.serialization.Example: private static final long serialVersionUID = 8528929994176475972L;
That's not the value of 123 that I specified. And in my slightly more complex example, the ObjectOuputStreams and ObjectInputStreams confirm that my specified values are being ignored.
Found the problem. Long
is not the same as long
; be sure when specifying a serialVersionUID that it matches the exact form:
private static final long serialVersionUID = [value]L;