So, a while ago I wrote a SerializableImage class in order to easily bundle several images together into a single file. The class worked fine, both serializing, file I/O and drawing.

In another project I'm working on I successfully used a copy of this SerializableImage class, until recently. I had decided that I wanted my project in a "real" package, rather than the default_package (due to some jar-ing problems). Now, all of a sudden, my SerializableImage class starts generating ClassNotFound exceptions when I try to cast the objects from my image file to SerializableImage.

Nothing in the class has changed except that I added

package myPackage;

to the header of my classes. Class structure and serialVersionUID are exactly the same. The class is found, since I'm able to instanciate SerializableImage objects.

Does anyone know a way to get around this problem, or will I have to admit defeat, change serialVersionUID and re-generate all my image bundles again with the new class version?

Thanks for your time!
Emil Olofsson

Are you saying that you saved the images to a file, then changed the class and then tried to read? If yes then Yes you will not be able to read them because:
You serialized instances of this: SerializableImage. the files containes this: SerializableImage.
But whan you read them you are trying to pass that to a: myPackage.SerializableImage instance.
This will compile:

myPackage.SerializableImage image = (myPackage.SerializableImage)readObject();

But if the read object doesn't return an myPackage.SerializableImage object the program will crash. And the files contain SerializableImage objects.

So you will have to serialize the images again. Or for a more "professional" solution: Write a prgram that reads all the files and saves them into a SerializableImage. Then use the values of those objects to create mypackage.SerializableImage objects and save those by replacing the old files. you will have to do the above only once and the files will work from now on.

the class changed, therefore any old serialised instances are now incompatible with the new class structure.

Yeah I figured. I had already tried adding myPackage as prefix, but with no luck. Luckily my image packaging program also generated txt-files containing the filenames of all images in a package in case something like this would happen, so repacking was easy.

Thanks for clearing this out for me!
Emil Olofsson

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.