Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Does Java support shutdown code when program is killed?

Thread view: 
nooneinparticular314159@yahoo.com - 05 May 2006 01:43 GMT
I want my program to print something when I terminate it by hitting
control-C.  Is this possible?  How do you do this in Java?

Thanks!
Rhino - 05 May 2006 04:57 GMT
>I want my program to print something when I terminate it by hitting
> control-C.  Is this possible?  How do you do this in Java?

You could write a key listener that prints whatever you want whenever CTRL-C
is hit.

See http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html 
for an article describing how to write a Key Listener.

--
Rhino
Thomas Schodt - 05 May 2006 05:56 GMT
> I want my program to print something when I terminate it by hitting
> control-C.  Is this possible?  How do you do this in Java?

<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook(j
ava.lang.Thread
)>

Not sure if System.out is guaranteed to be available at this point though.
Simon - 05 May 2006 07:23 GMT
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook(j
ava.lang.Thread
)>
>
> Not sure if System.out is guaranteed to be available at this point though.

I'm also not sure about whether or not availability of System.out is guaranteed
in any way, but I'm sure that it worked for me at least once. At least you can
be sure that it does not throw an exception since it is a PrintStream :-)

The docs of addShutdownHook say:

"Uncaught exceptions are handled in shutdown hooks just as in any other thread,
by invoking the uncaughtException method of the thread's ThreadGroup object. The
default implementation of this method prints the exception's stack trace to
System.err and terminates the thread; it does not cause the virtual machine to
exit or halt."

So they also assume that System.err is available.

Cheers,
Simon
Tony Morris - 05 May 2006 05:57 GMT
>I want my program to print something when I terminate it by hitting
> control-C.  Is this possible?  How do you do this in Java?
>
> Thanks!

You add a "shutdown hook" thread.

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

Signature

Tony Morris
http://tmorris.net/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.