简体   繁体   English

反序列化java.awt.Color-> java.io.InvalidClassException

[英]Deserialize java.awt.Color -> java.io.InvalidClassException

i want to serialize and deserialize an Object, which contains a color array. 我想序列化和反序列化一个对象,其中包含一个颜色数组。 (color[][]) (颜色[][])

The color array represents a small icon. 颜色数组代表一个小图标。

After some Builds i get an error when i try to deserialize the object: 经过一些构建后,当我尝试反序列化对象时出现错误:

java.io.InvalidClassException: java.awt.Color; local class incompatible: stream classdesc serialVersionUID = 118526816875918197, local class serialVersionUID = 118526816881161077

What can i do to fix that? 我该怎么解决? The Object itself has the default UID (serialVersionUID = 1L;)... 对象本身具有默认的UID(serialVersionUID = 1L;)...

Thank you :) 谢谢 :)

Take a look on the error message again: 再次查看错误消息:

java.io.InvalidClassException: java.awt.Color; 
local class incompatible: 
stream classdesc serialVersionUID = 118526816875918197, 
local class serialVersionUID = 118526816881161077

It seems that you serialized the class using one version of Color and try to deserialize it with other. 似乎您使用一个版本的Color序列化了该类,并尝试与另一个版本进行反序列化。

Here is the serialVersionUID in my environment. 这是我的环境中的serialVersionUID

 private static final long serialVersionUID = 118526816881161077L;

It matches your second serialVersionUID . 它与您的第二个serialVersionUID匹配。 However the first is different: 118526816875918197 . 但是第一个是不同的: 118526816875918197 This fact is a little bit strange because I compared this version ID with java 6 and java 7 JDK and they are equal. 这个事实有点奇怪,因为我将此版本ID与Java 6和Java 7 JDK进行了比较,它们相等。 I cannot compare it with older JDK as well as with JDK from other vendors (not from Oracle). 我无法将其与较旧的JDK以及其他供应商(而非Oracle)的JDK进行比较。 So I can assume that you created your file with either older version of JDK or with JDK from other vendor (eg open JDK, IBM etc.) or in other platform. 因此,我可以假设您是使用旧版本的JDK或其他供应商(例如,开放的JDK,IBM等)的JDK或在其他平台中创建的文件。

If this is the case I suggest you to customize serialization of your class by implementing your own writeObject() and readObject() methods. 如果是这种情况,我建议您通过实现自己的writeObject()readObject()方法来自定义类的序列化。 You do not have to re-implement serialization of all your classes. 您不必重新实现所有类的序列化。 It is enough to wrap Color with your custom wrapper customize serialization for it. 用您的自定义包装器自定义序列化包装Color就足够了。

BTW forward/backward compatibility of serialized object is a well known weakness of whole serialization mechanism of java. BTW序列化对象的向前/向后兼容性是Java整个序列化机制的一个众所周知的弱点。 You can use alternatives, eg serialization to JSON, XML or other binary format. 您可以使用其他方法,例如序列化为JSON,XML或其他二进制格式。

For JSON you can use Jackson or Gson, for XML JAXB or Xstream, for binary format protobuff. 对于JSON,您可以将Jackson或Gson用于XML JAXB或Xstream,用于二进制格式的原型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM