5
votes

I am using a class that implements Serializable from a jar, and to make sure there won't be compiler related issues I gave it a serialVersionUID private static final long serialVersionUID = 123L;

I then recreated the jar but now when I am using the class inside the new jar it is giving me the exception:

java.io.InvalidClassException: com.placeiq.piqhash.PIQDeviceId; local class incompatible: stream classdesc serialVersionUID = 1568630193381428614, local class serialVersionUID = 123

So my question is: 1, What is the stream classdesc serialVersionUID? 2, What can I do to set it so that the two serialVersionUID are the same?

Thanks!

2
Creating a jar file prints no such message. What code are you running when you get an error? If you change a version and then read old data with the class, it will fail. - bmargulies
Hi, @bmargulies it was not showing during the creating jar process, it was thrown when I was trying to write to it in my other project - Liang

2 Answers

4
votes

A serialized version of that class is coming from somewhere (in a stream) and it has a serial version ID of 1568... but it can't be deserialized to your class version (the local class) which is 123.

You need to figure out where that other version is coming from. Then you need to use your new jar, with the new version of the class where ever that is (if you can).

Some possible sources:

  • A network message from some other computer is coming into your program with a serialized instance of that class. (This could be raw socket messages or RMI or anything that uses Java serialization. It is not going to be a JSON message or XML message that gets mapped to that object.)

  • Another program or an older version of your program wrote an instance of the class to a file on disk (or otherwise withing the file system) and your program is not retrieving it.

  • Your program has two versions of the jar--an old one and a new one--that are present, usually because of different classloaders. (This can happen if you aren't careful to manage all the classpaths available on a Java EE container of some sort, including Tomcat or Jetty.)

This is not an exhaustive list.

0
votes

You have to set the serialVersionUID in your class to match that of the class your deserializing from. In this case, it ought to be 1568630193381428614, not 123L.

Though, I question the appropriateness of this approach. You may want to just reference the implementation provided by the vendor instead of trying to match the versions using a magic number. In particular, are you certain that you have implemented exactly the same properties and method behaviors?