aspose file tools*
The moose likes Java in General and the fly likes class invalid for deserialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "class invalid for deserialization" Watch "class invalid for deserialization" New topic
Author

class invalid for deserialization

Bud Tippins
Ranch Hand

Joined: Jan 28, 2011
Posts: 52
I have the following code. Running in NetBeans. I'm receiving an error that "class invalid for deserialization"

What do I need to do here to prepare the class for deserialization?

Thank you.


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Bud,

Hmmm. That error message is generally only going to come up in weird situations in which a class implements the Serializable interface, objects of the class are serialized, the class is then modified to not implement Serializable, and then you try to deserialize the contents of the file. Given that you're trying to extract Integers from the file, though, that explanation doesn't make sense, since it only applies if you're deserializing a user-supplied class.

So... how did you create the file foo.ser? What's really in it?


[Jess in Action][AskingGoodQuestions]
Bud Tippins
Ranch Hand

Joined: Jan 28, 2011
Posts: 52
Thanks for your help. Here is the code that I used to create foo.ser:

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Ernest Friedman-Hill wrote:That error message is generally only going to come up in weird situations in which a class implements the Serializable interface, objects of the class are serialized, the class is then modified to not implement Serializable, and then you try to deserialize the contents of the file.

Bingo!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Bud,

OK, that explains it, then! You saved a SaveBox in the file, and so Java is going to look for a class file named SaveBox to load when the file is read. But the SaveBox it finds when the class is read is not serializable, so this isn't going to work! You need to do two things differently:

1) To load foo.ser, you need SaveBox.class to exist, and (most importantly!) it needs to be the same class that was saved into the file -- i.e. the second one you showed me. Therefore, you're going to have to name your file reading class something else!

2) Once you fix that problem, your program will still fail. That's because you wrote a SaveBox, but you're trying to read two Integers. You need to read a SaveBox instead. Whatever you write ot the file, that's what you're going to get back out. So...


Your SaveBox class doesn't have getWidth and getHeight -- you'll need to add those.
Bud Tippins
Ranch Hand

Joined: Jan 28, 2011
Posts: 52
Ernest, Thanks for your help. I think I understood what you said. Here are the two updated files based on your comments. I'm still getting an error on lines 25 and 26 saying that it can't find the symbol svbox. The program is giving errors when I run it in NetBeans.

To write the file:



To read the file:


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
Right, svbox is known in main, but not in go. You could declare the go method to take a SaveBox parameter, and thus pass svbox to it.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Or drop svbox completely and use the local variable box instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bud Tippins
Ranch Hand

Joined: Jan 28, 2011
Posts: 52
Thank you for your help. O.K., I've declared the go method to take a SaveBox parameter. below is the code. Now I'm getting an error at runtime: "java.io.InvalidClassException: savebox.SaveBox; local class incompatible: stream classdesc serialVersionUID = -9024886691042451633, local class serialVersionUID = -1451760072563593720"

Also it's printing out that width and height are both = 0. They were set as 20 and 50 when I saved SaveBox.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
java.io.InvalidClassException: savebox.SaveBox; local class incompatible: stream classdesc serialVersionUID = -9024886691042451633, local class serialVersionUID = -1451760072563593720

Did you make any changes to the SaveBox class after you created the .ser file? Try creating and reading a new .ser file.

Also it's printing out that width and height are both = 0. They were set as 20 and 50 when I saved SaveBox.

That's just a consequence of deserialization being unsuccessful.
Bud Tippins
Ranch Hand

Joined: Jan 28, 2011
Posts: 52
Ulf, I recreated the savebox file and it worked properly. Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class invalid for deserialization