Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

serialver?

Thread view: 
CapCity - 28 Nov 2006 21:14 GMT
OK, experienced developer, but new to Java (and eclipse, which is another
hurdle until I get used to it).

I inherited a project that has several classes that implement Serializable
and have a serialVersionUID variable. I created a new class that is a clone
of several of these existing ones and want to generate a serialVersionUID
for it. Looking on-line, I found several descriptions for serialver. I've
had no luck with it. I keep getting "class xxx not found" messages.

The file is a few folders deep, say src/dir1/dir2/dir3/myclass. From what
folder do I execute serialver and what do I use as the class name? The
package it belongs to would be dir1.dir2.dir3

Found an eclipse plug-in, but says the feature is "not available here", or
something like that.

I think an issue may be that these are all in .java files and not .class
files. But all the ones with serialVersionUIDs are .java files.

Also, do I need to generate a new one everytime the class changes?

BTW, the person who originally developed this doesn't remember how they were
generated.

Thanks!
ducnbyu - 28 Nov 2006 22:05 GMT
> OK, experienced developer, but new to Java (and eclipse, which is another
> hurdle until I get used to it).
[quoted text clipped - 21 lines]
>
> Thanks!

Perhaps there's a better way, but...

In Eclipse when I clone a class that implements Serializable, to get a
new serial version number I delete the line that declares the serial
version UID and then a little plus-sign or something appears (maybe I
have to save to get this) next to the class declaration line... I think
I double-click that or right click it (I don't pay too much attention)
and I get a list of things to do, one of which is to create a new
serial version number UID.  Selecting it recreates the line with a new
number.
EJP - 29 Nov 2006 05:31 GMT
You need to run serialver from the directory which contains the topmost
package, or you can specify the classpath on the command line.

> Also, do I need to generate a new one everytime the class changes?

On the contrary, you should *never* change it. If you introduce
serialization incompatibilities you should use one of the various
mechanisms available so that the readObject() method will understand
serializations from older versions of the class.
Lew - 02 Dec 2006 07:38 GMT
> You need to run serialver from the directory which contains the topmost
> package, or you can specify the classpath on the command line.
[quoted text clipped - 5 lines]
> mechanisms available so that the readObject() method will understand
> serializations from older versions of the class.

Joshua Bloch in _Effective Java_, Item 55, "Consider Using a Custom Serialized
Form":

"If you ever want to make a new version of the class that is /incompatible/
with existing versions, merely change the [serialVersionUID] value in the
declaration. This will cause attempts to deserialize serialized instances of
previous versions to fail with an InvalidClassException."

A class will be incompatible with earlier versions if you added or removed
serializable (non-transient) members.

Never say never.

- Lew
sgoo - 02 Dec 2006 12:29 GMT
I think the main usage of the serialver tool is keeping compatibility,
so that JRE believes the new class with the serialVersionUID field is
still the old one.
Thus, if you're creating a new class, you can just use any constant as
the serialVersionUID. I prefer the version number.
EJP - 03 Dec 2006 09:36 GMT
> Never say never.

I agree, but the Java forums are full of horror stories people who have
followed incorrect advice in this matter and have found themselves with
serialized databases which they cannot recover. Better to preserve
compatibility where and when you can rather than introduce it
unnecessarily, and the consequences of changing the serialVersionUID on
every class change are precisely that.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.