which event can i catch, when my application is closed by the system?

Hello,

I have an application that I put in the taskbar. I want the application to react (write in a log file), when it's being closed.

with an actionListener, I can catch the event when the user terminates the application.

but which event can I catch, when the system closes the application when it shuts down?

I allready tried to make a JFrame of it and put a windowListener on it, to catch windowClosing, but that doesn't work.

Any Idea?

For the moment I use the following code .

--CODE SNIPLET-

Logger(){

TrayIcon trayIcon = null;

if (SystemTray.isSupported()) {

// get the SystemTray instance

SystemTray tray = SystemTray.getSystemTray();

// load an image

Image image = Toolkit.getDefaultToolkit().getImage(strImage);

// create a popup menu

PopupMenu popup = new PopupMenu();

// create menu items for the default action

MenuItem exitItem = new MenuItem("Exit");

exitItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { closing();}

});

popup.add(exitItem);

// construct a TrayIcon

trayIcon = new TrayIcon(image, "Logger", popup);

// add the tray image

try {

tray.add(trayIcon);

} catch (AWTException e) {

System.err.println(e);

}

}

}

public void closing() {

String text = "something to say";

writeLogEntry(text);

System.exit(0);

}

Message was edited by:

thomasfournier

[1578 byte] By [thomasfourniera] at [2007-11-27 1:42:28]
# 1
This seems to be the right solution.Why aren't you happy whit this?You need tot do something more?
J@A@V@Aa at 2007-7-12 0:59:08 > top of Java-index,Desktop,Core GUI APIs...
# 2

well, with my actual solution, the method "closing()" is called when the user clicks on the Popup-Menu-Entry "Exit".

But I want "closing()" to be called also when the system shuts down, and terminates all running processes.

(I.e.: I want to log at what time the system shuts down).

thomasfourniera at 2007-7-12 0:59:08 > top of Java-index,Desktop,Core GUI APIs...
# 3
But you can make the OS do this job
J@A@V@Aa at 2007-7-12 0:59:08 > top of Java-index,Desktop,Core GUI APIs...
# 4
hi!you are right. there is a concept named shutdown hook.. search for that in google and in other places. i hope you will get your answer. if not then please get back. i will try to help you ....regardsAniruddha
Aniruddha-Herea at 2007-7-12 0:59:08 > top of Java-index,Desktop,Core GUI APIs...
# 5
Yes, Aniruddha is right. Look at Runtime class ApiDocumetation. There is the addShutdownHook method thatwill be useful for you.Bye
CanapaGa at 2007-7-12 0:59:08 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thanks alot, Aniruddha-Here! That's what I need.
thomasfourniera at 2007-7-12 0:59:08 > top of Java-index,Desktop,Core GUI APIs...