尝试反序列化对象时出错。

huangapple 未分类评论55阅读模式
标题翻译

Error when I try to deserialize an object

问题

  1. 我和我的朋友正在使用GitHub进行编码在我们更新主类时IDE提供了以下错误
  2. Exception in thread "main" java.io.InvalidClassException: Packages.contoCorrente; local class incompatible: stream classdesc serialVersionUID = -433050874178120905, local class serialVersionUID = -5213999116280532749
  3. at java.base/java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:689)
  4. at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1958)
  5. at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1827)
  6. at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2115)
  7. at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1646)
  8. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:464)
  9. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
  10. at java.base/java.util.ArrayList.readObject(ArrayList.java:928)
  11. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  12. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  13. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  14. at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  15. at java.base/java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1160)
  16. at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2271)
  17. at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2142)
  18. at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1646)
  19. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:464)
  20. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
  21. at ObjectWrite.main(ObjectWrite.java:31)
  22. 为了解决这个问题我总是删除主对象文件以便它可以重新构建我还附上了处理反序列化的代码部分希望有人能帮助我
  23. public static void main(String[] args) throws Exception {
  24. ArrayList<contoCorrente> contiCorrentiArray;
  25. File file = new File("contiCorrenti");
  26. if (file.exists()) {
  27. FileInputStream fileIn = new FileInputStream(file);
  28. ObjectInputStream fileObjIn = new ObjectInputStream(fileIn);
  29. //noinspection unchecked
  30. contiCorrentiArray = (ArrayList<contoCorrente>) fileObjIn.readObject();
  31. fileObjIn.close();
  32. fileIn.close();
  33. } else {
  34. contiCorrentiArray = new ArrayList<>();
  35. }
  36. System.out.println("Numero Conti Presenti: " + contiCorrentiArray.size() + "\n");
  37. }
英文翻译

I am coding using GitHub with my friend, and when we update the main class, the IDE provides us the following error:

  1. Exception in thread &quot;main&quot; java.io.InvalidClassException: Packages.contoCorrente; local class incompatible: stream classdesc serialVersionUID = -433050874178120905, local class serialVersionUID = -5213999116280532749
  2. at java.base/java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:689)
  3. at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1958)
  4. at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1827)
  5. at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2115)
  6. at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1646)
  7. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:464)
  8. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
  9. at java.base/java.util.ArrayList.readObject(ArrayList.java:928)
  10. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  11. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  12. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  13. at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  14. at java.base/java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1160)
  15. at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2271)
  16. at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2142)
  17. at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1646)
  18. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:464)
  19. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
  20. at ObjectWrite.main(ObjectWrite.java:31)

To fix the problem, I always delete the main object file, so it can rebuild it. I am also attaching the part of code that deals with deserialization, in the hope that someone will help me.

  1. public static void main(String[] args) throws Exception {
  2. ArrayList&lt;contoCorrente&gt; contiCorrentiArray;
  3. File file = new File(&quot;contiCorrenti&quot;);
  4. if (file.exists()) {
  5. FileInputStream fileIn = new FileInputStream(file);
  6. ObjectInputStream fileObjIn = new ObjectInputStream(fileIn);
  7. //noinspection unchecked
  8. contiCorrentiArray = (ArrayList&lt;contoCorrente&gt;) fileObjIn.readObject();
  9. fileObjIn.close();
  10. fileIn.close();
  11. } else {
  12. contiCorrentiArray = new ArrayList&lt;&gt;();
  13. }
  14. System.out.println(&quot;Numero Conti Presenti: &quot; + contiCorrentiArray.size() + &quot;\n&quot;);

答案1

得分: 1

你和你的伴侣是否拥有相同的 serialVersionUID?

如果不是的话,你们两个都应该定义它,以防止它被自动计算。

根据文档:

如果一个可序列化的类没有显式地声明serialVersionUID,那么序列化运行时将根据类的各个方面计算出一个默认的serialVersionUID值,如Java(TM)对象序列化规范中所述。然而,强烈建议所有可序列化的类都显式地声明serialVersionUID值,因为默认的serialVersionUID计算对编译器实现可能因类细节而异,因此在反序列化过程中可能会导致意外的InvalidClassExceptions。因此,为了保证在不同的Java编译器实现之间具有一致的serialVersionUID值,可序列化的类必须声明一个显式的serialVersionUID值。还强烈建议在可能的情况下,显式的serialVersionUID声明使用private修饰符,因为这样的声明仅适用于立即声明的类 - serialVersionUID字段作为继承成员是没有用的。数组类不能声明显式的serialVersionUID,因此它们始终具有默认计算值,但是对于数组类,匹配的serialVersionUID值的要求被放宽了。

英文翻译

Do you and your Partner have the same serialVersionUID?

If not, you both should define it, so it doesn't get calculated automatically.

From the docs:

> If a serializable class does not explicitly declare a
> serialVersionUID, then the serialization runtime will calculate a
> default serialVersionUID value for that class based on various aspects
> of the class, as described in the Java(TM) Object Serialization
> Specification. However, it is strongly recommended that all
> serializable classes explicitly declare serialVersionUID values, since
> the default serialVersionUID computation is highly sensitive to class
> details that may vary depending on compiler implementations, and can
> thus result in unexpected InvalidClassExceptions during
> deserialization. Therefore, to guarantee a consistent serialVersionUID
> value across different java compiler implementations, a serializable
> class must declare an explicit serialVersionUID value. It is also
> strongly advised that explicit serialVersionUID declarations use the
> private modifier where possible, since such declarations apply only to
> the immediately declaring class--serialVersionUID fields are not
> useful as inherited members. Array classes cannot declare an explicit
> serialVersionUID, so they always have the default computed value, but
> the requirement for matching serialVersionUID values is waived for
> array classes. `

huangapple
  • 本文由 发表于 2020年3月16日 21:02:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/60706519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定