Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ashokformac

macrumors member
Original poster
Mar 26, 2007
34
0
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 ...
 

4409723

Suspended
Jun 22, 2001
2,221
0
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.
 

Helium3

macrumors newbie
Oct 8, 2005
3
0
Runtime

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

macrumors 6502
May 17, 2002
283
0
Denver, CO
Depending on exactly what you're trying to accomplish adding a shutdown hook can get you at least partially there:

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

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

ashokformac

macrumors member
Original poster
Mar 26, 2007
34
0
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
 

ashokformac

macrumors member
Original poster
Mar 26, 2007
34
0
I tried it but that shutdown hook method was not calling when i stops the program using Stop button in the IDE .....
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.