Post

Java Deep-Dive: Navigating the Sea of InvalidClassException

Hello Java coding community, today we bring you an in-depth exploration of a commonly encountered error - the InvalidClassException. We are going to dive into the intricacies of this anomaly and arm you with effective solutions. Without any further ado, let’s dive into dissecting InvalidClassException in Java.

What is Java’s InvalidClassException?

InvalidClassException is a type of ObjectStreamException that arises when the Java Virtual Machine (JVM) attempts to deserialize an object and finds that the class of the object is incompatible with the serialized form.

This issue can occur due to many reasons, often related to changes to the class that were not compatible with serialization, including:

  • If a serializable or externalizable class has been changed incompatibly; type of a serializable field has been changed to an incompatible type.
  • If a serializable or externalizable class doesn’t have available no-arg constructor.
  • If the SUID (SerialVersionUID) of the class doesn’t match the SUID of the serialized object, and so on.

This exception is usually accompanied by a detailed message explaining the context in which this error occurred.

Let’s look at a code snippet that throws this exception:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.company;

import java.io.*;

class Demo implements Serializable {
    int a;
    long b;
}
public class Main {

    public static void main(String[] args) {
        
        Demo object = new Demo();
        String filename = "file.ser";
        
        // Serialization
        try {
            FileOutputStream file = new FileOutputStream(filename);
            ObjectOutputStream out = new ObjectOutputStream(file);
            out.writeObject(object);
            out.close();
            file.close();
        }
        catch(IOException ex) {
            System.out.println("IOException is caught");
        }

        // Deserialization without Serializable field
        object = null;
        try {
            FileInputStream file = new FileInputStream(filename);
            ObjectInputStream in = new ObjectInputStream(file);
            object = (Demo)in.readObject();
            in.close();
            file.close();
        }
        catch(IOException ex) {
            System.out.println("IOException is caught");
        }
        catch(ClassNotFoundException ex) {
            System.out.println("ClassNotFoundException is caught");
        }

        System.out.println("a = " + object.a);
        System.out.println("b = " + object.b);        
    }
}

Here, the IOException arises due to the occurrence of InvalidClassException.

How to Solve InvalidClassException?

One of the ways to resolve this issue is by assigning a static final serialVersionUID of any access modifier to your class. The serialVersionUID is a unique identifier for each class and JVM uses it to compare the versions of a class ensuring that the same class was used during Serialization is loaded during Deserialization.

Here’s an example of how it can be done:

1
2
3
4
5
6
7
8
9
10
package com.company;

import java.io.*;

class Demo implements Serializable {
    static final long serialVersionUID = 42L;
    int a;
    long b;
}
//..., the rest of the code remains same.

This will ensure that the JVM understands the version and recognizes the class during the Deserialization, hence, resolving the InvalidClassException.

Conclusion

Tackling and resolving exceptions are an integral part of any Java programmer’s journey. InvalidClassException is no less but understanding its origins and the ways to resolve it can save a lot of time during code debugging. Remember, serializing and deserializing require attention to details like serialVersionUID and compatible changes.

Stay tuned for more such deep dive articles on Java anomalies and happy coding!

This post is licensed under CC BY 4.0 by the author.