serialversionUID does not match

烂漫一生 提交于 2019-12-13 13:43:20

问题


I am fairly new to java and I have a class Products that is Serializable. I do not know what I did, but my programs stopped working and gave me this error :

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

I searched around and found ways to declare it, but that doesn't help me right now. I even went as far as opening a new project and just copying the text in each class from the non working project and pasting it in the new project, but i still get the same error. I am getting this error across all of my projects (they use the same Serializable class) I need to know what i did to do this so it doesn't happen again, and what i can do to fix it. I already tried to declare the serialversionUID and that did not work.

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

回答1:


Put this line in your class and everything should be ok!

private static final long serialVersionUID = -558553967080513790L;

That's because you implements Serializable interface in the Product class and if you do not define serialVersionUID in the specific class, there's no guarantee that different machines use the same id also in different versions of class the autogenerated serialVersionUIDs will also be different. Actually The default serialVersionUID computation is highly sensitive to class details and may vary from different JVM implementation, and result in an unexpected InvalidClassExceptions during the deserialization process.

Edited Read this -- Serializable interface's docs:

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.

Good Luck!



来源:https://stackoverflow.com/questions/43828566/serialversionuid-does-not-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!