serialver?

C

CapCity

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!
 
D

ducnbyu

CapCity said:
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!

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.
 
E

EJP

CapCity wrote:

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.
 
L

Lew

EJP said:
CapCity wrote:

You need to run serialver from the directory which contains the topmost
package, or you can specify the classpath on the command line.


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.

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
 
S

sgoo

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.
 
E

EJP

Lew said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top