Re: Classes generated by eclipse vs javac have different serialVersionUID
[post re-ordered]
<tai.thach@gmail.com> wrote in message
news:1154105914.264461.303100@i3g2000cwc.googlegroups.com...
Oliver Wong wrote:
<tai.thach@gmail.com> wrote in message
news:1154023941.784072.253160@s13g2000cwa.googlegroups.com...
Hi,
I'm using eclipse 3.2 for development and have configured my project to
use JDK 1.5.0_07 with Compiler compliance level 1.3 (using default
compliance settings) and selected all "Classfile Generation" options
except "Inline finally blocks." The problem I'm having is that the
class files that are generated by eclipse are not the same as those
generated by javac (also using JDK 1.5.0_07), which is used in a make
script. javac is invoked like so:
javac -source 1.3 -g -d classes -nowarn -deprecation -classpath
<classpath> ...
The different class files cause a problem because I have serialized
some objects already with the javac classes so when I try to
deserialize them with the eclipse classes, I get the
InvalidClassException. When I run serialver on the offending class
file, I get two different values for the class file generated with
javac and the one generated using eclipse.
Has anyone ever had this or some similar problem? How do I correct this
(besides not using the eclipse class files)? I've spent many hours
scouring the web to find a solution, but have had no success. Any help
would be really appreciated!
Isn't it possible to directly specify the serialversionUID by
declaring
the appropriate static field?
It is possible to specify the serialVersionUID, however, if I do that I
have to make sure that it is exactly the same as the original class
file. Although I can find the old class file and get its
serialVersionUID, I am betting that there is no other version out there
that may have had a different UID. I think this is a fairly safe bet,
but as this is a product that has shipped to numerous customers, I
cannot make this change without handling all the different scenarios.
This would involve much more code and testing, which is not currently
possible for me.
From the JavaDocs:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html
<quote>
it is strongly recommended that all serializable classes explicitly declare
serialVersionUID values, since the default serialVersionUID computation is
highly sensitive to class details that may vary depending on compiler
implementations, and can thus result in unexpected InvalidClassExceptions
during deserialization. Therefore, to guarantee a consistent
serialVersionUID value across different java compiler implementations, a
serializable class must declare an explicit serialVersionUID value.
</quote>
Also, AFAIK, the Java serialization mechanism is not intended for long
term storage of objects, so if you need to serialize and deserialize objects
across different versions of your application, you may be in for a lot of
headaches.
I'm not sure that there's a quick fix for this problem. You may have to
do some re-design work.
- Oliver