1

My app worked, and after I changed my names to English version I got InvalidClassEception. How can I solve it? I found some information about it, but I don't understand how can I solve it. Some help? Here is my code:

public class Serializare {
    //private static final long serialVersionUID = 1L;
    public void serialize(Bank bank, String fileName) {
        try (ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream(fileName))) {
            out.writeObject(bank);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public Bank deserializeW(String fileName){
        Bank bank = null;
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                fileName))) {
            bank = (Bank) in.readObject();
        } catch (IOException | ClassNotFoundException ex) {
            ex.printStackTrace();

        }
        return bank;
    }
}

public class Bank implements BankProc, Serializable {

    /**
     * 
     */
    //private static final long serialVersionUID = -7029959057180382645L;
    private Hashtable<Integer, Account> bank;

    ...

}

My error was

java.io.InvalidClassException: Bank.Bank; local class incompatible: stream classdesc serialVersionUID = -7029959057180382645, local class serialVersionUID = -2329932680711902869
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
nnn
  • 13
  • 2
  • Can you provide the exact error? It often includes useful information that can help to pin down the offending bit(s) of code. – CollinD May 17 '18 at 19:13
  • May be you used a `protected word` in your translation. No `protected words` in italian! – lalengua May 17 '18 at 19:15
  • java.io.InvalidClassException: Bank.Bank; local class incompatible: stream classdesc serialVersionUID = -7029959057180382645, local class serialVersionUID = -2329932680711902869 – nnn May 17 '18 at 19:15
  • When did you get the exception? Were you restarting a web application or communicating between two processes? – Timir May 17 '18 at 19:28
  • bank = (Bank) in.readObject(); This is the line where my code crushes.. it's a simple application which implements the operations of a client at a bank. – nnn May 17 '18 at 19:32
  • It's not a web application or a communication between two processes – nnn May 17 '18 at 19:34
  • 1
    Java serialization depends on the class name and field names and types being the same (or compatible, in the case of types) between serialization and deserialization. It is very fragile, and should almost never be used for any purpose whatsoever except by experts, and even then, XML, JSON, or almost any other external representation is probably a better choice. It sucks, basically. – David Conrad May 17 '18 at 20:06
  • It sucks indeed. There’s even a design pattern to keep things working across file structural changes, referred to as the serialization proxy pattern. For your case, a file serialized with older class names cannot be deserialized with new names. – Timir May 17 '18 at 20:15

1 Answers1

1

You tried to serialize a class with a given serialVersionUID and deserialize it with a different one.

See what is a serialVersionUID.

bruno
  • 2,213
  • 1
  • 19
  • 31