Problem in writing hashtable to file system.

M

M

Hi All,

I have problem in writing a hashtable to the file system
and hope you can give me some hints. The problem is like
this:

My hashtable consists of a key of type "String" and the
value is a object of a class WorkNature:
class WorkNature {
String responsible;
Vector duties;
}

It seems that I can write data to the hashtable (myHashTable)
but when I try to put it to the file system, there's some
problem. The code I used was:

void writeSysFile() {
final String PHY_FILE = "d:/Work_Nature";
try {
ObjectOutputStream os =
new ObjectOutputStream(new FileOutputStream(PHY_FILE));
os.writeObject(myHashTable);
os.close();
} catch (Exception e) {
System.out.println("writeSysFile: " + e.getLocalizedMessage());
}
}

Error "writeFile: myJavaProgram$WorkNature" was displayed !

Any ideas?
Thanks.
 
M

M

Andrew Thompson said:
...
} catch (Exception e) {
e.printStackTrace();
}
....

produces the kind of much more detailed output
that you can see an example of, here..
<http://www.physci.org/codes/javafaq.jsp#exact>

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Well, after changing the catch to e.printStackTrace(),
my program displayed something like:

java.io.NotSerializableException: myJavaProgram$WorkNature
at
java.io_ObjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
at
java.io_ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at java.util.Hashtable.writeObject(Hashtable.java:806)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
..java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at
java.io_ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:809)
at
java.io_ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1296)
at
java.io_ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1247)
at
java.io_ObjectOutputStream.writeObject0(ObjectOutputStream.java:1052)
at
java.io_ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at myJavaProgram.writeSysFile(myJavaProgram.java:61)

Woo, that's totally too technically for me!
Any clue? Thanks.
 
L

Lukas Weibel

Of course the whole content of a hashtable needs to be serializable.
Therefore your WorkNature implementing Serializable:

class WorkNature implements Serializable {
String responsible;
Vector duties;
}
 
M

M

Lukas Weibel said:
Of course the whole content of a hashtable needs to be serializable.
Therefore your WorkNature implementing Serializable:

class WorkNature implements Serializable {
String responsible;
Vector duties;
}

Thanks. But I'm new to Java and never heard about Serializable...
The Java API doc is too complicated to me.. Do you mind
give me some brief explanation?
 
C

Chris Smith

M said:
Thanks. But I'm new to Java and never heard about Serializable...
The Java API doc is too complicated to me.. Do you mind
give me some brief explanation?

http://java.sun.com/docs/books/tutorial/essential/io/serialization.html

I'm trying to understand how you managed to write code specifically for
the purpose of doing serialization, and yet never managed to hear about
Serializable. Nevertheless, that URL should help fill in the blanks.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

John C. Bollinger

M said:
Well, after changing the catch to e.printStackTrace(),
my program displayed something like:

java.io.NotSerializableException: myJavaProgram$WorkNature
at
java.io_ObjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
at
java.io_ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at java.util.Hashtable.writeObject(Hashtable.java:806)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at
java.io_ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:809)
at
java.io_ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1296)
at
java.io_ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1247)
at
java.io_ObjectOutputStream.writeObject0(ObjectOutputStream.java:1052)
at
java.io_ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at myJavaProgram.writeSysFile(myJavaProgram.java:61)

Woo, that's totally too technically for me!

Then you'd best give up programming. It's one thing to not understand
what you see there, and indeed I wouldn't necessarily expect a novice to
understand it immediately, but if it's really "too technical" then you
are studying the wrong subject. In case it might not really be over
your head, here's how to read that:

An exception of type java.io.NotSerializableException and specifying a
detail message of "myJavaProgram$WorkNature" was thrown by the method
writeObject0 of class java.io_ObjectOutputStream, specifically from the
code at line 1054 in the class' source. That method was invoked by the
writeObject method of java.io_ObjectOutputStream, at line 278 in the
class' source. THAT method was invoked [...]. Ultimately you get down
to the place in your own code that was involved: the writeSysFile method
of class myJavaProgram, at line 61 in your source.
Any clue? Thanks.

This exception is thrown when an object is required to be Serializable,
but isn't. The detail message specifies the name of the class of the
not Serializable object: myJavaProgram$WorkNature, an inner class of
myJavaProgram with unqualified name "WorkNature". That class can be
made Serializable simply by declaring that it implements the
java.io.Serializable interface.

Go ahead and try that to see what happens. My guess is that the file
you write will not be what you want, in that it will not be
human-readable. If you want to produce a human-readable listing of the
contents of your Hashtable then you will need to control the formatting
yourself in one way or another. For you, that probably means extracting
the Hashtable entries one by one, and writing an appropriate string to
the output file for each.


John Bollinger
(e-mail address removed)
 
L

Lukas Weibel

Not at all. We all started small once.

With Serializable you mark (Seriablizable is a so call marker interface,
it does not expose any methods) a class as serializable. This has
something to do with security. Consider a bad person can serialize your
objects and read their content in the file system.

You can find further information at
http://java.sun.com/j2se/1.4.2/docs/guide/serialization/index.html.

Regards
Lukas
 
C

Chris Smith

M said:
Woo, that's totally too technically for me!
Any clue? Thanks.

Interpreting exception stack trace output is a fundamental skill that
will be important to you in doing any software development in Java.
There are three parts you care about:

1. The exception class
2. The detail message
3. The stack itself

In your case:
java.io.NotSerializableException: myJavaProgram$WorkNature

The exception is java.io.NotSerializableException. You can look that up
in the API documentation, and read all about this exception.

The detail message is "myJavaProgram$WorkNature". The exact meaning of
the detail message depends on which exception class you're dealing with.
In the case of NotSerializableException, the detail message tells you
the class name of the object that isn't serializable. It's your
WorkNature class (which is apparently a nested class inside of a class
called myJavaProgram; as an aside, myJavaProgram should really have been
called MyJavaProgram to avoid confusion).

The rest is the stack trace, which tells you where the exception
occurred. You can generally skip lines until you see something that's
your own code. So here we go:
at
java.io_ObjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
at
java.io_ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at java.util.Hashtable.writeObject(Hashtable.java:806)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at
java.io_ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:809)
at
java.io_ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1296)
at
java.io_ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1247)
at
java.io_ObjectOutputStream.writeObject0(ObjectOutputStream.java:1052)
at
java.io_ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at myJavaProgram.writeSysFile(myJavaProgram.java:61)

And there you go. So to find the code that caused this exception, look
at line number 61 of myJavaProgram.java.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

M

Lukas Weibel said:
Not at all. We all started small once.

With Serializable you mark (Seriablizable is a so call marker interface,
it does not expose any methods) a class as serializable. This has
something to do with security. Consider a bad person can serialize your
objects and read their content in the file system.

You can find further information at
http://java.sun.com/j2se/1.4.2/docs/guide/serialization/index.html.

Regards
Lukas

Thanks.
After I put the "... implements Serializable" stuff in my class,
it still give me the same error...

Anything I might done wrong?
Thx.
 
C

Chris Smith

M said:
After I put the "... implements Serializable" stuff in my class,
it still give me the same error...

Anything I might done wrong?

Are you sure it's the same error? If so, then you probably still have
an old copy of the class file for WorkNature sitting around somewhere.

If the error is a little different, then it might be something else.
Specifically, I suspect you might be misunderstanding the relationship
between WorkNature and myJavaProgram; if WorkNature is an inner class,
you need to ask yourself if that's really necessary, and if so how to
handle that in serialization (do you make myJavaProgram serializable,
for example, or something else).

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
V

VisionSet

M said:
Hi All,

I have problem in writing a hashtable to the file system
and hope you can give me some hints. The problem is like
this:

My hashtable consists of a key of type "String" and the
value is a object of a class WorkNature:
class WorkNature {
String responsible;
Vector duties;
}

It seems that I can write data to the hashtable (myHashTable)
but when I try to put it to the file system, there's some
problem. The code I used was:

void writeSysFile() {
final String PHY_FILE = "d:/Work_Nature";
try {
ObjectOutputStream os =
new ObjectOutputStream(new FileOutputStream(PHY_FILE));
os.writeObject(myHashTable);
os.close();
} catch (Exception e) {
System.out.println("writeSysFile: " + e.getLocalizedMessage());
}
}

Error "writeFile: myJavaProgram$WorkNature" was displayed !

Change your code.
Do not catch Exception, catch only the subclasses that are thrown.
ie IOException etc. That's just good practise.
In your catch block, put:
e.printStackTrace() for more meaningful debugging info.
 
A

Andrew Thompson

Woo, that's totally too technically for me!

...hhhmmmm.. What John and Chris said in relation
to that comment was alot more useful, but less funny,
than what I would have responded.

Maybe it is best they got to it first.
Any clue? Thanks.

I take you back to..
<http://www.physci.org/codes/javafaq.jsp#exact>

Which, given it was written by me, pretty
much sums up the best advice I can muster
on general tips for solving exceptions.

There is a group that might be better for you
at this stage. A group where folks are a great
deal more patient and helpful..
<http://www.physci.org/codes/javafaq.jsp#cljh>
 
M

M

Well, I have added "implements Serializable" to the class WorkAround.
But I haven't put "implements Serializable" to the calling class.

Is it necessary?
 
C

Chris Smith

M said:
Well, I have added "implements Serializable" to the class WorkAround.
But I haven't put "implements Serializable" to the calling class.

I don't know, mainly because I don't know what class WorkAround is. It
doesn't seem to have any relation to any of the relevant classes at all,
so chances are it doesn't need to be serializable.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Andrew Thompson

For the most part, pretty much the same people follow both groups, in my
experience.

Yes, but AFAIAC, it's smart questions or
merciless ridicule on c.l.j.programmer.
OTOH, When I post to c.l.j.h., I take
more care to be 'patient and helpful'.
Some others do as well.

The difference is not the *names* of the
people, but the 'way they hold their faces'
when answering questions. ;-)
 

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,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top