Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Urgent..Need some help.

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I want to close my Java Application in an orderly fashion when Windows is "Shut down".
How can I do that using Java. I tried using addShutdownHook() from class Runtime. But that does not seem to work.
Anyone has any suggestions for this??
Please Help..
Thanks.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What operating system are you using....because i found when using that type of hook, win 98 will hang at shutdown where as with xp or 2000 it will shutdown properly.
If you are using 98 then try using the javaw command for some reason it shuts the program no prob.
Hope that will help
 
Riddhi Joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for getting back to me.
I am using Windows 2000. I tried to run
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
//stuff to do;
}
});
This code did not work under Wind 2k Pro. However, I want my software to be enviornment independent. So I am in search of a method of API that resolves problem for Win98 or any other Windows OS.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Riddhi,
You need to carefully read the API docs on Runtime.addShutdownHook(). Many things can go bump in the night when a system is attempting to go down. Especially look at these two paragraphs:
Shutdown hooks run at a delicate time in the life cycle of a virtual machine and should therefore be coded defensively. They should, in particular, be written to be thread-safe and to avoid deadlocks insofar as possible. They should also not rely blindly upon services that may have registered their own shutdown hooks and therefore may themselves in the process of shutting down.
Shutdown hooks should also finish their work quickly. When a program invokes exit the expectation is that the virtual machine will promptly shut down and exit. When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.
Michael Morris
 
Riddhi Joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Micheal,
Thanks for responding.
I had read that earlier when I was going through the API of Runtime.
Actually, I have this software that needs to be closed when windows is shut down unorderly.
Hence I was thinking of writing this method and in the run method of the same, I could write window closing to exit the program.
I am not sure how to do that and if that is the correct way to do it.
Any suggestions?. I am very much new to all this so really need your guidance.
Thanks.
 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want a similar solution, did you find anything Rishi?

Maki Jav
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,

I got solution in this post

Thanx,

Maki Jav
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic