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
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;
}
void writeRulesPackageEntry(JarOutputStream os) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
RulesPackaging rulesPackaging = new RulesPackaging();
rulesPackaging.exportResources(this.rules, output);
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
this.writeEntry(os, Product.ENTRY_RULES_PACKAGE, input);
}
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();
jar = new JarFile(new File(filename));
} catch (IOException e) {
e.printStackTrace();
}
return jar;
}