简体   繁体   中英

ClassNotFoundException in reading object from GZipped file

A *.gz file was created under a previous version of a package I'm using (it has the class tratz.semantics.ClassificationBundle). In the current version, the class is named miacp.semantics.ClassificationBundle. The two have identical members. Is there any way to make the earlier version readable in the later version? The basic read function is

ClassificationBundle bundle = (ClassificationBundle) ois.readObject();

I have the earlier version, so I can read the file in that version. But, how would I then create a new file that can be read in the new version?

Java binary serialization is very punishing with any sort of refactoring. Unfortunately, tratz.semantics.ClassificationBundle can never be deserialized as miacp.semantics.ClassificationBundle .

JSON or XML serialization are common solutions to this sort of problem. Alternatively, you can create a customized storage format. Or you can simply store the primitive/simple data values and then load those instead of loading a custom object (int/long/double/String/...etc).

Put tratz.semantics.ClassificationBundle class back with readResolve() method that will create your new object. When all serialized content is de-serialized in a new format (as a new class) you will be able to remove this old stub.

I followed flakes suggestion with success, putting both packages in the classpath. It is hopefully useful to describe the details. Using Netbeans , I created another source path to tratz and added a subfolder and copied the original ClassificationBundle class to that folder. Netbeans told me the other packages I needed to import, so I was able to copy those as well, to the appropriate subfolders. I then went back to the original code cited above to read the object. I continued to get ClassNotFoundException for other classes. This led to the need to copy still other classes. Eventually, I copied 57 classes and was able to read the GZipped file successfully. The next step is write the utility to copy the content (much simpler, but still with some difficulty, given the complexity of the data structure).

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM