views:

280

answers:

2

I would like to change my qestion to the following:

I have one HashMap object, and one int. I want to serialize the into the same file and get back. I know that I should do deserialization in the same order as I have done serialization.

Please provide me with a code which performes that action.

+4  A: 

I am making an assumption based on the information you have provided that you are having serialization issues based on incompatibilities between the originally serialized class and an updated class with a new field.

If you have previously serialized a Class without specifying a private static long serialVersionUID, adding a new field will result in an InvalidClassException when trying to read the file back.

Note that simply adding the serialVersionUID after you have already serialized the previous version will not result in a compatible class. What you need to do is regenerate the original serialVersionUID, grab that number and apply it to your class.

The Steps you must take:

  1. remove the new int (for now) and compile
  2. generate the original serialVersionUID by running serialVer against the compiled class
  3. take the resulting number and assign it to the private static long serialVersionUID that you are applying to your updated class
  4. re-add the new int.

After these steps, you should be ok to deserialize the original.

recommended reading: why should I bother about serialVersionUID (sic)

akf
If you have existing data, you need to set the `serialVersionUID` to that of the version of the class that created the data (use the serialver tool).
Tom Hawtin - tackline
(If you've lost the original class files, you should be able to find another question on stackoverflow that explains how you can go about recovering them.)
Tom Hawtin - tackline
+1  A: 

Without a stack trace, it is hard to guess the exact problem, but I'm going to go out on a limb and suggest two possibilities:

The hashmap at runtime doesn't contain serializable objects. Given the generic type that you present, this would only be possible if a raw type was used. Such as:

public void setMap(HashMap map) { this.map = map; }

That would give you a compiler warning, but it would compile so something like that may be going on. The same thing would happen if you had a getter on the map, and something referenced the map using its raw type. Say:

public Map<String, String> getMap() { return map; }

Map m = getMap();
m.put(new Object(), new Object());

The other possibility I can think of is that you are corrupting the file in some way when you write or read it (say doing something with file encoding or not closing the stream when you write it).

Without more information (the complete stack trace - cut out the lines referencing your own project name if you need, but no others) and the code which does the serialization and deserialization, it is impossible to get further insight into the problem.

By the way, no comment or answer thinks that you serialized a HashMap and want to deserialize MyClass (it isn't really possible - you can't decide what class you get when you deserialize). I think you are misunderstanding the comments. What they are talking about is different versions of MyClass as they have existed over time. I'm guessing in my answer that this is not the case (you are in fact serializing MyClass fresh during the running of the code and deserializing without any recompilation in-between. If not (if in fact you serialized the class, changed it, and recompiled and are trying to re-read an old file), then akf's answer with Tom's comments are on the right track.

Yishai