gpt4 book ai didi

java - serialversionUID 不匹配

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:03:41 26 4
gpt4 key购买 nike

enter image description here

我是 java 的新手,我有一个 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

我四处搜索并找到了声明它的方法,但现在这对我没有帮助。我什至甚至打开了一个新项目,只是从非工作项目中复制每个类中的文本并将其粘贴到新项目中,但我仍然遇到同样的错误。我在我的所有项目中都遇到了这个错误(它们使用相同的 Serializable 类)我需要知道我做了什么来避免再次发生,以及我可以做些什么来修复它。我已经尝试声明 serialversionUID 但没有成功。

产品类别

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());
}
}

最佳答案

将这一行放在你的类中,一切都应该没问题!

private static final long serialVersionUID = -558553967080513790L;

那是因为你在Product类中实现了Serializable接口(interface),如果你没有在具体的类中定义serialVersionUID,就不能保证不同的机器在不同版本的类中使用相同的 id,自动生成的 serialVersionUID 也会不同。实际上,默认的 serialVersionUID 计算对类细节高度敏感,并且可能因不同的 JVM 实现而异,并在反序列化过程中导致意外的 InvalidClassExceptions。

已编辑阅读此 -- 可序列化接口(interface)的文档:

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 - serialversionUID 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43828566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com