java.io.invalidClassException during serializing/deserializing

后端 未结 3 1796
再見小時候
再見小時候 2020-12-07 02:45

I\'ve got an object that im reading and writing to and from fileinputstreams/objectinputstreams and objectinputstreams/objectoutputstreams. I keep

相关标签:
3条回答
  • 2020-12-07 02:57

    As stated in documentation this can happen for three different reasons:

    • The serial version of the class does not match that of the class descriptor read from the stream
    • The class contains unknown datatypes
    • The class does not have an accessible no-arg constructor

    So, first of all check that both implementations have the same serialVersionUID. If this is true you have to be sure that the class doesn't use any type that is undefined (or unknown) to the JVM you are trying to deserialize into. Finally you need to provide a standard constructor ClassName() which does empty initialization.

    These can be the problems and surely it's one of these, so I don't think you should look forward something weird. From my personal experience I can add also that using different JVM versions to serialize and deserialize can create this problem, so be sure of it too.

    0 讨论(0)
  • 2020-12-07 03:05

    Are you reading from a file? In that case, it does not matter if you added the serialVersionUID now, it is different from the one stored in the file, and that creates the exception.

    A quick solution could be to set serialVersionUID to 4209360273818925922L, which seems to be the serialVersionUID that was automatically generated by java when you saved those object in that file at that time :)

    0 讨论(0)
  • 2020-12-07 03:15

    You see the first line?
    You have differenct serialVersionUIDs, theay don't are compatible with each other, you have to use the same serialVersionUID!

    java.io.InvalidClassException:

    com.luxurymode.pojos.Reminder; Incompatible class (SUID): com.luxurymode.pojos.Reminder: static final long serialVersionUID =4209360273818925922L; but expected com.luxurymode.pojos.Reminder: static final long serialVersionUID =1L;

    0 讨论(0)
提交回复
热议问题