We help IT Professionals succeed at work.

how can I implement my finalize()?

KNVB HK
KNVB HK asked
on
Medium Priority
181 Views
Last Modified: 2013-11-24
My java servlet write will a temp. file when it running.
However,I want to delete the temp. file on my program exit. So, would you tell me how implement my finalize() function?

Also, do the java garbage collector runs my finalize function automatically?
Comment
Watch Question

Commented:
Don't implement it in finalize(), because it's not 100% called by gc.

Do write it in destroy(). This will be called by the servlet container the servlet is being taken out of service.
KNVB HKSITO
CERTIFIED EXPERT

Author

Commented:
well! what can I do?
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Simply call the deletOnExit() method of the File object corresponding to your temp file and the file will be automagically deleted when the JVM exits.
KNVB HKSITO
CERTIFIED EXPERT

Author

Commented:
Hey object!

can you tell me more detail on it?
KNVB HKSITO
CERTIFIED EXPERT

Author

Commented:
Do you mean call the deletOnExit() method of the file object, then the system will delete the file upon jvm exit?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Exactly :)

From the javadoc:

"Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification (12.9).
Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used with care."

KNVB HKSITO
CERTIFIED EXPERT

Author

Commented:
I tested this method. It works!. The file is deleted when I close the browser. However, it doesn't work when I change file name to variable. can you tell me why?
I have check that the generated cannot be delete when I close the browser.
KNVB HKSITO
CERTIFIED EXPERT

Author

Commented:
I tested this method. It works!. The file is deleted when I close the browser. However, it doesn't work when I change file name to variable. can you tell me why?
I have check that the generated cannot be delete when I close the browser.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> when I change file name to variable

I'm not sure what you mean, can you explain more please and I'll try and help.
KNVB HKSITO
CERTIFIED EXPERT

Author

Commented:
Just careless mistake, forget it.
By the way,
I found that I need restart the Jrun server to force the system to delete file.
Is this possible to delete the file when the user close the browser.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Yes that's because the it is the servlets JVM that handles deleting the file.
If you want it deleted when the browser closes then you'll need to get your applet to invoke a servlet (when it's stop() or delete() method is called) that'll handle the deletion explicitly. But this'll also delete the file whenever the applet is stopped/destroyed, not just when the browser closes. Not sure if this is desired behaviour for you.

KNVB HKSITO
CERTIFIED EXPERT

Author

Commented:
thank very much Object
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
no worries :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.