GeeCON Prague 2014*
The moose likes Java in General and the fly likes Is there any way to close all existing file handlers when the java application ends? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Is there any way to close all existing file handlers when the java application ends?" Watch "Is there any way to close all existing file handlers when the java application ends?" New topic
Author

Is there any way to close all existing file handlers when the java application ends?

cle tan
Ranch Hand

Joined: Jun 11, 2012
Posts: 68
As sometimes when the application ends, some of the logs will be xxx.log.1 , xxx.log.2
seems like the logs are still being locked.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39103
    
  23
You would usually close files immediately after reading or writing, but I see that log files would stay open until the JVM exits.
Can you list all open files?
What about a shutdown hook which starts a new thread to close all those files?
cle tan
Ranch Hand

Joined: Jun 11, 2012
Posts: 68
Assuming java application program ended, can it guarantee that a file won't be locked when it exits?

how to implement a shutdown hook?
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Any decent operating system will free the resources held by a program upon its termination.
Exactly what kind of lock is it? Does it persist across the life span of a program?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39103
    
  23
Shutdown hooks are really easy to use. If you search, you find lots of links, of which I am showing two: 1 2. I think no 1 is better.
 
GeeCON Prague 2014
 
subject: Is there any way to close all existing file handlers when the java application ends?