Search code examples
javadatabaseserializationserializable

Java - InvalidClassException local class incompatible serialVersionUID


I created a client application that gets some data from the database (Oracle) and serialized them. With some data (happens once in 100), when serialized a data, fails with this exception:

Exception in thread "main" java.io.InvalidClassException: org.jdom.Element; local class incompatible: stream classdesc serialVersionUID = -5756298698047880134, local class serialVersionUID = -1584223699423688446
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:621)   at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:621)

The data are inserted in the database with other application.

Looking at Google, I see that maybe should define a serialVersionUID. I do it (Im not entirely sure that it´s correctly) but is not resolved. My code:

public class Test1{

/(private static final long serialVersionUID = -5756298698047880134L;//-1584223699423688446L

public static void main(String[] args) throws Exception {
    // TODO code application logic here
    ExecutionContext ret = null;

    System.out.println("Connection with BD...");

    String url = "url";
    String user = "user";
    String password = "password";
    Connection conn = DriverManager.getConnection(url, user, password);

    System.out.println("Connection ok...");
    System.out.println("Create Query...");

    String query = "SELECT ... FROM ...";

    Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);

    ResultSet rs = st.executeQuery(query);

    if (rs.next()) {
        Blob blob = rs.getBlob("EXECUTION_CONTEXT");

        if (blob == null) {
            System.out.println ("Blob null");
        }


        try{
            InputStream is = blob.getBinaryStream();
            GZIPInputStream gzip = new GZIPInputStream(is);
            ObjectInputStream os = new ObjectInputStream(gzip);

            //Here is where fail:
            ret = (ExecutionContext)os.readObject();
            //
        }catch(ZipException ie){
            if(ie.getMessage().equals("Not in GZIP format")){
                System.out.println("Not in GZIP format");
                InputStream is = blob.getBinaryStream();
                ObjectInputStream os = new ObjectInputStream(is);

                //Here is where fail:
                ret = (ExecutionContext) os.readObject();
                //
            }else{
                JOptionPane.showMessageDialog(null, "Error when extracting data");
            }

        }

    }
}

}

Any idea?

EDIT with the solution:

In the ExecutionContext class that I pick the serialize object, in this class must implement Serializable and define serialVersionUID.

ret = (ExecutionContext) os.readObject();

Solution

  • The serialVersionUID needs to be defined with the same value as in the stream: -5756298698047880134.

    You must have deployed two different versions of the JDOM library.