简体   繁体   English

Java中2个不同JVM上类的对象的序列化

[英]Serialization of an Object of a class on 2 different JVMs in java

I have a class say Counter. 我有一堂课,说Counter。 I serialize Object of this class on JVM-1 in one machine & transfer the serialized Object as a network stream to another machine & deserialize there in JVM-2. 我在一台机器上在JVM-1上序列化此类的对象,然后将序列化的对象作为网络流传输到另一台机器上,并在JVM-2中反序列化。 Will this Object get deserialized properly ? 该对象会正确反序列化吗? Would there be any error/exception thrown in this case? 在这种情况下会抛出任何错误/异常吗? Assuming i have compiled the class on both JVMs. 假设我已经在两个JVM上编译了该类。 I am assuming that there is no serial Version UID present in the classes 我假设类中不存在串行版本UID

只要两端都运行相同的代码/ jdk(尽管可以使用不同的jdk),则序列化/反序列化将不是问题。

if you have serialVersionUID in your class, serialize/deserialize will not be a problem, but if serialVersionUID is missing in your code, as you are compiling class with both JVM(I understand, .class file has same content but compiled with different JVMs) in that case, serialVersionUID is assigned by java, which won't be same, hence serialize/deserialize will not work. 如果您的类中有serialVersionUID,则序列化/反序列化将不是问题,但是如果您的代码中缺少serialVersionUID,则因为您正在使用两个JVM编译类(我知道,.class文件具有相同的内容,但使用不同的JVM编译)在这种情况下,serialVersionUID由java分配,这将是不一样的,因此序列化/反序列化将不起作用。 So, add 因此,添加

private static final long serialVersionUID = -6903933977591709194L;

in case you haven't added, with any value, and compile as many times, rest assured, serialize/deserialize will work. 如果您没有添加任何值,并且可以进行多次编译,请放心,序列化/反序列化将起作用。 :) :)

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. 序列化运行时与每个可序列化的类关联一个版本号,称为serialVersionUID,在反序列化期间使用该版本号来验证序列化对象的发送者和接收者是否已加载了该对象的与序列化兼容的类。 If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. 如果接收方已为该对象加载了一个与相应发送方类具有不同的serialVersionUID的类,则反序列化将导致InvalidClassException。 A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long: 可序列化的类可以通过声明一个名称为“ serialVersionUID”的字段来显式声明其自己的serialVersionUID,该字段必须是静态的,最终的且类型为long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 11L; ANY-ACCESS-MODIFIER静态最终长serialVersionUID = 11L;

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. 如果可序列化的类未明确声明serialVersionUID,则序列化运行时将根据该类的各个方面,为该类计算默认的serialVersionUID值,如Java(TM)对象序列化规范中所述。 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. 但是,强烈建议所有可序列化的类显式声明serialVersionUID值,因为默认的serialVersionUID计算对类详细信息高度敏感,而类详细信息可能会根据编译器的实现而有所不同,因此可能在反序列化期间导致意外的InvalidClassExceptions。 Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. 因此,为了保证不同Java编译器实现之间的serialVersionUID值一致,可序列化的类必须声明一个显式的serialVersionUID值。 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. 还强烈建议显式serialVersionUID声明在可能的情况下使用private修饰符,因为此类声明仅适用于立即声明的类-serialVersionUID字段作为继承成员没有用。 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,因此它们始终具有默认的计算值,但是对于数组类,无需匹配serialVersionUID值。

If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. 如果接收方已为该对象加载了一个与相应发送方类具有不同的serialVersionUID的类,则反序列化将导致InvalidClassException。 A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long 可序列化的类可以通过声明一个名称为“ serialVersionUID”的字段来显式声明其自己的serialVersionUID,该字段必须是静态的,最终的且类型为long

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

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