java.io.InvalidClassException: 本地类不兼容: [英] java.io.InvalidClassException: local class incompatible:

查看:23
本文介绍了java.io.InvalidClassException: 本地类不兼容:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了客户端和服务器,然后在客户端添加了一个类用于序列化目的,然后只需转到我硬盘中客户端的文件夹并将其复制粘贴到服务器的相应位置,classname.classclassname.java 分别.

I created client and server and then added a class in client side for serializing purposes, then simply just went to the folder of the client in my hard drive and copy paste it to the server correponding location, both classname.class and classname.java respectively.

它在我自己的笔记本电脑上运行良好,但是当我想在其他系统上继续工作时,当我打开项目文件夹并在客户端尝试连接到服务器后,出现以下错误:

It worked well in my own laptop but when I wanted to continue my work on other system , when I opened the projects folders and after client tries to connect to the server, the following error appears:

Exception in thread "main" java.io.InvalidClassException: projectname.clasname; local class incompatible: stream classdesc serialVersionUID = -6009442170907349114, local class serialVersionUID = 6529685098267757690
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:562)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1582)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1495)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)

这是怎么回事?是不是因为我用旧版本的 IDE 运行程序?

What is going on? Is it because I ran the program with an older version of the IDE?

import java.io.Serializable;
import java.net.URL;

public class KeyAdr implements Serializable {
  private static final long serialVersionUID = 6529685098267757690L;

  public URL adr;
  public String key;
}

推荐答案

如果一个类没有在代码中明确定义一个private static final long serialVersionUID 它将自动生成,并且没有任何保证不同的机器会产生相同的id;看起来这正是发生的事情.此外,如果类有任何不同(使用不同版本的类),自动生成的 serialVersionUID 也将不同.

If a class does not explicitly define a private static final long serialVersionUID in the code it will be autogenerated, and there is no guarantee that different machines will generate the same id; it looks like that is exactly what happened. Also if the classes are different in any way (using different versions of the class) the autogenerated serialVersionUIDs will also be different.

来自 Serializable 接口的 docs:

如果可序列化类没有显式声明 serialVersionUID,那么序列化运行时将根据类的各个方面计算该类的默认 serialVersionUID 值,如在 Java(TM) 对象序列化规范中进行了描述.但是,强烈建议所有可序列化的类都明确声明 serialVersionUID 值,因为默认的 serialVersionUID 计算对可能变化的类细节高度敏感取决于编译器的实现,因此可能会在反序列化期间导致意外的 InvalidClassExceptions.因此,为了保证在不同的 Java 编译器实现中具有一致的 serialVersionUID 值,可序列化的类必须声明一个显式的 serialVersionUID 值.还强烈建议显式 serialVersionUID 声明尽可能使用 private 修饰符,因为此类声明仅适用于立即声明的类——serialVersionUID字段作为继承成员没有用.数组类不能声明显式的 serialVersionUID,因此它们始终具有默认的计算值,但数组类无需匹配 serialVersionUID 值.

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.

你应该在类定义中定义一个serialVersionUID,例如:

You should define a serialVersionUID in the class definition, e.g.:

class MyClass implements Serializable {
    private static final long serialVersionUID = 6529685098267757690L;
    ...

这篇关于java.io.InvalidClassException: 本地类不兼容:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆