PDA

View Full Version : java automatic shutdown




ashokformac
Apr 19, 2007, 01:27 AM
when my java program shutdowns automatically or shutdowns when the system shut downs or or when systems crashes ,,, i want to perform some actions like updating database....

how can i do this ...

thanks in advance ...



Wes
Apr 19, 2007, 12:24 PM
when my java program shutdowns automatically or shutdowns when the system shut downs or or when systems crashes ,,, i want to perform some actions like updating database....

how can i do this ...

thanks in advance ...

Your second requirement seems nigh on impossible. When the system crashes it stops responding and is really in a bad state. There's no real way to have the system call some java program when it has crashed, and even if you did, I doubt that it would in be much of a state to start the JVM.

The other feature you want is possible, but that's what Google is for.

LtRammstein
Apr 19, 2007, 12:26 PM
Wouldn't be better to just do an Autosave kinda deal with it than having to elaborate on "shutdown"?

Steve

Wes
Apr 19, 2007, 12:27 PM
Wouldn't be better to just do an Autosave kinda deal with it than having to elaborate on "shutdown"?

Steve

Without a doubt.

Helium3
Apr 19, 2007, 12:52 PM
You can at least partially get what you want by using the addShutdownHook(Thread hook) method on the java.lang.Runtime class. Take a look at the documentation on the Sun website for more information.

ryan
Apr 19, 2007, 12:55 PM
Depending on exactly what you're trying to accomplish adding a shutdown hook can get you at least partially there:


Runtime.getRuntime().addShutdownHook(new ShutdownHookThread());

private class ShutdownHookThread extends Thread {
public void run() {
System.out.println("Application shutdown");
}
}

ashokformac
Apr 20, 2007, 12:12 AM
Just i want to perform some action when my program stopped externally...
ie..when stopped by IDE itself or Closing my system tray icon or closing from dock .....

how can i do this ...

thanks in advance

Wes
Apr 20, 2007, 07:33 AM
Not sure why you don't read the post directly above of yours. The answer is blatantly there, but seeing as you need somebody to hold your hand:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)

ashokformac
Apr 21, 2007, 01:52 AM
I tried it but that shutdown hook method was not calling when i stops the program using Stop button in the IDE .....