13

I have several serializable classes that were compiled without specifying a serialVersionUID. I now need to add some data members to these classes but wish to preserve some already serialized objects.

Is there anyway to find out the serialVersionUID of these serialized objects so that I can specify the same ID in the source code?

3 Answers 3

14

If you haven't modified the code yet you could probably run the serialver tool (part of the JDK) and see what it generates. I believe that will get you what the calculated seriVersionUID is.

Here is a brief article on serialver.

Sign up to request clarification or add additional context in comments.

3 Comments

Make sure you do this with the exact same JVM that you used to originally compile the code!
This should also work on a different JVM, because otherwise class files and serialized objects would not be portable across JVMs (which they are).
The Java Object Serialization spec, section 4.6, explains how serialVersionUID may not be compatible across JVMs in the case of inner-classes, due to implementations generating different names for synthetic members. I've seen this happen in practice, even with two Sun JVMs.
6

What I usually do is, I compile the code, try to run it, and the raised exception mentions the offending SVUID, so I can cut and paste it in my own code. Crude, but it works.

Comments

3

If you are using Eclipse, it has a "quick fix" to generate the calculated serialVersionUID. This of course only works if you have the (exact) source for the classes at the time the objects have been serialized.

If you have at least the class files for them, use the "serialver" command line tool (comes with JDK).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.