24

Is there a way to determine the generated serialVersionUID of a serialized Java object?

The problem is that I serialized an object without explicitely specifying the serialVersionUID. Now the deserialization process complains about class incompatibilities. However I didn't change the class in a way which would make it incompatible. So I assume that it is enough to specify the serialVersionUID in the class as it is stored in the object data. In order to do this I need to read the serialVersionUID from the serialized data.

Daniel Fortunov
  • 43,309
  • 26
  • 81
  • 106
paweloque
  • 18,466
  • 26
  • 80
  • 136
  • What happens in the case when a method is added to the class? Can the older serialized object be read using the modified serialized class? – sprezzatura Sep 24 '10 at 11:59
  • Methods does not constitute the state of an object, so no problem with reading the serialized object unless you are not using any serialVersionUID.. – Amitesh Rai Feb 20 '15 at 18:05

9 Answers9

33

This works for me:

long serialVersionUID = ObjectStreamClass.lookup(YourObject.getClass()).getSerialVersionUID();

Hope it helps you too.

Martins
  • 1,231
  • 2
  • 14
  • 17
27

There's an easy way to find out serialversionUID of a class-

Suppose you have class in which you have forgotten to mention serialversionUID-

import java.io.Serializable;

public class TestSerializable implements Serializable {

}

Just do this-

serialver -classpath . TestSerializable

This prints-

static final long serialVersionUID = 5832063776451490808L;

serialver is a utility that comes along with JDK

Keshav
  • 4,408
  • 8
  • 31
  • 50
  • 3
    But this only works if you have the old version of class TestSerializable. lewap's problem is that he doesn't know what the value of serialVersionUID was with an old version of the class of the object he serialized. If he doesn't have to old version of the class anymore, he can't use serialver to find out. – Jesper Aug 24 '09 at 13:25
  • that's what version control is for. – james Aug 24 '09 at 13:54
  • Good answer but it isn't working on Android if the class imports framework types (like Context) – FindOut_Quran Sep 01 '18 at 07:10
13

You can do this by extending ObjectInputStream:

public class PrintUIDs extends ObjectInputStream {

  public PrintUIDs(InputStream in) throws IOException {
    super(in);
  }

  @Override
  protected ObjectStreamClass readClassDescriptor() throws IOException,
      ClassNotFoundException {
    ObjectStreamClass descriptor = super.readClassDescriptor();
    System.out.println("name=" + descriptor.getName());
    System.out.println("serialVersionUID=" + descriptor.getSerialVersionUID());
    return descriptor;
  }

  public static void main(String[] args) throws IOException,
      ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    List<Object> list = Arrays.asList((Object) new Date(), UUID.randomUUID());
    oos.writeObject(list);
    oos.close();
    InputStream in = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new PrintUIDs(in);
    ois.readObject();
  }

}

I believe it would be possible to read all the serialized data by replacing the descriptor returned by the method, but I haven't tried it.

McDowell
  • 107,573
  • 31
  • 204
  • 267
5

There is metadata associated with the serialized bits (a header if you like). You can read the value from the metadata if you know at which position it is (the SerialVersionUID is written there along with other info such as the class name).

I think this article might help you: The Java serialization algorithm revealed.

Note that the bits are written "in clear" (unless you encrypted the stream explicitly) so a HEX editor might be all you need to see what is the SerialVersionUID.

1

There is a specified grammar for the serialization of objects:

See chapter 6.4 in http://java.sun.com/javase/6/docs/platform/serialization/spec/serialTOC.html

Using this, you should be able to determine the SerialVersionUID of your serialized object.

WMR
  • 12,661
  • 5
  • 35
  • 30
1

enter image description here for Android developers,

you can also enable serialization inspection from Settings -> Editor -> Inspections -> enable "Serializable class without 'serialVersionUID' check" first, then ALT+ ENTER, the studio will create serialVersionUID for you.

shanwu
  • 1,493
  • 6
  • 35
  • 45
1

The easiest way to get it (especially on hard to find cases) is to define any number for serialVersionUID and monitor the stacktrace when the deserialization fails, it will print out the original one.

FindOut_Quran
  • 728
  • 3
  • 10
  • 27
0

That's exactly what you should do - specify your own static final long serialVersionUID.

There's a section about it in the docs for Serializable.

Unless you've specified a serialVersionUID I don't believe there's an easy way to get it other than deciphering the stream as @WMR suggests.

Nick Holt
  • 33,455
  • 4
  • 52
  • 58
  • Note that it should be named serialVersionUID (not: serialversionuid - Java is case-sensitive), and it should be private static final long). – Jesper Aug 24 '09 at 12:28
  • I agree. But is there a way to read the generated uid from the serialized data? – paweloque Aug 24 '09 at 12:28
0

there is much simpler way to get the serialVersionUID of serialized object - just deserialize it with apache commons serialization utils to same class with different serial version uid and read the exception message which will be more or less:

org.apache.commons.lang.SerializationException: java.io.InvalidClassException: my.example.SerializableClass; local class incompatible: stream classdesc serialVersionUID = -1, local class serialVersionUID = -2

psychollek
  • 323
  • 3
  • 10