What can change SerialVersionUID during serialization and storage in Jarfile?

I have to deal with some problems when serializing objects (I use JBoss Drools and want to keep ArrayList of KnowledgePackage).

When I serialize the list, save the result in a file and deserialize it, the problem does not occur, so it works fine.

But when I serialize the list, save the result in a byte stream, and then save it in a JarFile, I cannot then deserialize the result due to this error:

IOException during package import : java.util.ArrayList; local class incompatible: stream classdesc serialVersionUID = 8664875232659988799, local class serialVersionUID = 8683452581122892189

So I think the problem is that I am storing the serialized object in a Jarfile entry. I think I'm doing it right, because other files stored in the same way in the Jarfile can be read correctly. And after using 'cmp' and 'hexdump', I noticed that saving it in the bank causes a change in one octet if uuid, otherwise the contents will be the same.

I am really disappointed and cannot indicate where the problem may be.

What can change SerialVersionUID between two classes? other than vm version?


add source code: exportToJar -> writeRulesPackageEntry -> writeEntry

/**
 * Writes content provided from a reader into a file contained in a jar.
 * 
 * @param output the output stream to write on
 * @param entryName the name of the file that will contain reader data
 * @param contentReader 
 * 
 * @return the zip entry that has been created into the jar
 */
ZipEntry writeEntry(JarOutputStream output, String entryName, ByteArrayInputStream input) {
    if (output == null || entryName == null || entryName.trim().length() == 0 || input == null) {
        throw new NullPointerException("Null argument passed");
    }

    ZipEntry entry = new ZipEntry(entryName);
    byte[] buffer = new byte[BUFFER_LENGTH];

    try {
        output.putNextEntry(entry);
        int nRead;

        while ((nRead = input.read(buffer, 0, BUFFER_LENGTH)) > 0) {
            output.write(buffer, 0, nRead);
        }

        output.closeEntry();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return entry;
}

/**
 * Export rules files to a serialized object (ArrayList<KnowledgePackage>) into 
 * an output stream, then write the output content as an entry of a jar.
 * 
 * @param os the output jar to write in
 */
void writeRulesPackageEntry(JarOutputStream os) {
    // serialize objects and write them to the output stream
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    RulesPackaging rulesPackaging = new RulesPackaging();
    rulesPackaging.exportResources(this.rules, output);

    // create a new input stream to read written objects from
    ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    this.writeEntry(os, Product.ENTRY_RULES_PACKAGE, input);
}

/**
 * Creates a JarFile containing resources. 
 * 
 * @param filename the exported jar filename
 * @return the jar as an object, null if an error occured
 */
public JarFile exportToJar(String filename) {
    FileOutputStream fOs;
    JarOutputStream jOs;
    JarFile jar = null;

    try {
        fOs = new FileOutputStream(filename);
        jOs = new JarOutputStream(fOs);

        this.writeRulesPackageEntry(jOs);

        jOs.close();

        // construct a jar from the output jar
        jar = new JarFile(new File(filename));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return jar;
}
+3
source share
3 answers

serialVersionUID . static final, ( , ), .

http://mindprod.com/jgloss/serialization.html.

, serialVersionUID java.util.ArrayList - 8683452581122892189L, , 1.2.

, , , , JarFile - , , .

, java.io.InputStreamReader.

JavaDoc:

InputStreamReader - : . .

, , , , , (. , ). java.io.ByteArrayInputStream, java.io.InputStreamReader writeRulesPackageEntry(JarOutputStream). char[] .

+3

Nick, , ( ), ( ).

, - , (1997), " JavaBeans". , 11 "" , . PDF http://ccd.uab.es/~srobles/manuals/JavaBeans

0

, JarFile, ? , () "" .

The reason I ask is because I saw similar error messages using my caching system (EHCache) when I updated the serialized class but did not reset the old persistent cache.

0
source

Source: https://habr.com/ru/post/1712006/


All Articles