Java - InvalidClassException本地类不兼容的serialVersionUID

时间:2014-12-09 09:20:25

标签: java database serialization serializable

我创建了一个客户端应用程序,它从数据库(Oracle)获取一些数据并对其进行序列化。对于一些数据(在100中发生一次),当序列化数据时,会因此异常而失败:

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)

将数据与其他应用程序一起插入数据库中。

看看谷歌,我看到也许应该定义一个serialVersionUID。我这样做(我不完全确定它是正确的)但是没有解决。我的代码:

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");
            }

        }

    }
}

}

有什么想法吗?

使用解决方案

编辑

在我选择serialize对象的ExecutionContext类中,在此类中必须实现Serializable并定义serialVersionUID。

ret = (ExecutionContext) os.readObject();

1 个答案:

答案 0 :(得分:0)

serialVersionUID需要使用与流中相同的值进行定义:-5756298698047880134

您必须部署了两个不同版本的JDOM库。