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

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

问题描述

我创建了客户端和服务器,然后在客户端添加了一个类用于序列化目的,然后只是到我的硬盘驱动器中的客户端文件夹,并将其粘贴到服务器相应的位置, classname.class classname.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 )对象序列化规范。但是,强烈建议所有可序列化类显式声明 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:local类不兼容:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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