0

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();
Jose Antonio
  • 578
  • 1
  • 8
  • 34

1 Answers1

0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I defined the serialVersionUID in my Class (I put it above in the code) but is not resolved. Should I pass my serialVersionUID to the readObject method? How? – Jose Antonio Dec 09 '14 at 09:31
  • Then you didn't deploy your changed and recompiled code. Try again. You don't need to do any more than this. You can tell by the value printed for `local class serialVersionUID` what the actual value was. – user207421 Dec 09 '14 at 09:44
  • I do a "clear & Build" and when run the application I have the same exception. If I print serialVersionUID, shows the value that I specified in my Test1 class (implements Serializable), but the error is the same... Above in the code you can see how I specify the serialversionUID. – Jose Antonio Dec 09 '14 at 12:25
  • Ok I see the problem. In the class ExecutionContext is where I must define the serialVersionUID. Thanks you. – Jose Antonio Dec 09 '14 at 12:53
  • According to the error message it is the class `org.jdom.Element`. – user207421 Dec 09 '14 at 22:12