serialversionUID 不匹配
serialversionUID does not match
我是 java 的新手,我有一个 class Products
Serializable
。我不知道我做了什么,但我的程序停止工作并给了我这个错误:
Exception in thread "main" java.io.InvalidClassException: cockeb.Product;
local class incompatible: stream classdesc serialVersionUID = 1288455942608122525,
local class serialVersionUID = -558553967080513790
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:616)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1829)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1713)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1986)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at java.util.ArrayList.readObject(ArrayList.java:791)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1058)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2122)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2013)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422
我四处搜索并找到了声明它的方法,但现在这对我没有帮助。我什至甚至打开了一个新项目,只是从非工作项目中复制每个 class 中的文本并将其粘贴到新项目中,但我仍然遇到同样的错误。我在我的所有项目中都遇到了这个错误(他们使用相同的 Serializable
class)我需要知道我做了什么来避免再次发生,以及我能做些什么来修理它。我已经尝试声明 serialversionUID
但没有成功。
产品Class
package cockeb;
import java.io.Serializable;
import java.math.BigDecimal;
public class Product implements Comparable<Product>, Serializable {
private String upc;
private String shortDetails;
private String longDetails;
private BigDecimal price;
private Integer stock;
public String getUpc() {
return upc;
}
public void setUpc(String upc) {
this.upc = upc;
}
public String getShortDetails() {
return shortDetails;
}
public void setShortDetails(String shortDetails) {
this.shortDetails = shortDetails;
}
public String getLongDetails() {
return longDetails;
}
public void setLongDetails(String longDetails) {
this.longDetails = longDetails;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
@Override
public int compareTo(Product t) {
return this.getUpc().compareTo(t.getUpc());
}
}
将此行放入您的class,一切都应该没问题!
private static final long serialVersionUID = -558553967080513790L;
那是因为你在Product
class中实现了Serializable
接口,如果你没有在具体的class中定义serialVersionUID
,则无法保证不同的机器在不同版本的 class 中也使用相同的 id,自动生成的 serialVersionUID 也会不同。
实际上 默认的 serialVersionUID 计算对 class 细节高度敏感,并且可能因不同的 JVM 实现而异,并在反序列化过程中导致意外的 InvalidClassExceptions。
已编辑阅读此 -- 可序列化接口的文档:
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.
祝你好运!
我是 java 的新手,我有一个 class Products
Serializable
。我不知道我做了什么,但我的程序停止工作并给了我这个错误:
Exception in thread "main" java.io.InvalidClassException: cockeb.Product;
local class incompatible: stream classdesc serialVersionUID = 1288455942608122525,
local class serialVersionUID = -558553967080513790
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:616)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1829)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1713)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1986)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at java.util.ArrayList.readObject(ArrayList.java:791)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1058)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2122)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2013)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422
我四处搜索并找到了声明它的方法,但现在这对我没有帮助。我什至甚至打开了一个新项目,只是从非工作项目中复制每个 class 中的文本并将其粘贴到新项目中,但我仍然遇到同样的错误。我在我的所有项目中都遇到了这个错误(他们使用相同的 Serializable
class)我需要知道我做了什么来避免再次发生,以及我能做些什么来修理它。我已经尝试声明 serialversionUID
但没有成功。
产品Class
package cockeb;
import java.io.Serializable;
import java.math.BigDecimal;
public class Product implements Comparable<Product>, Serializable {
private String upc;
private String shortDetails;
private String longDetails;
private BigDecimal price;
private Integer stock;
public String getUpc() {
return upc;
}
public void setUpc(String upc) {
this.upc = upc;
}
public String getShortDetails() {
return shortDetails;
}
public void setShortDetails(String shortDetails) {
this.shortDetails = shortDetails;
}
public String getLongDetails() {
return longDetails;
}
public void setLongDetails(String longDetails) {
this.longDetails = longDetails;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
@Override
public int compareTo(Product t) {
return this.getUpc().compareTo(t.getUpc());
}
}
将此行放入您的class,一切都应该没问题!
private static final long serialVersionUID = -558553967080513790L;
那是因为你在Product
class中实现了Serializable
接口,如果你没有在具体的class中定义serialVersionUID
,则无法保证不同的机器在不同版本的 class 中也使用相同的 id,自动生成的 serialVersionUID 也会不同。
实际上 默认的 serialVersionUID 计算对 class 细节高度敏感,并且可能因不同的 JVM 实现而异,并在反序列化过程中导致意外的 InvalidClassExceptions。
已编辑阅读此 -- 可序列化接口的文档:
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.
祝你好运!