• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write a signal handler(like in C) in Java?

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my Java application, I need to capture the CTRL+C signal. I know that in the C language, I can write a Signal handler to do this task. Does anybody know how to do this in JAVA? Thanks in advance.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a GUI based application or a console application? If it is GUI based, just use a KeyEvent and KeyListener and catch it that way. Let me know what type of app and I can explain in more detail.
 
liang gu
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a backend server application invoked from a UNIX command prompt like in the following:
/home/myaccount> Java myApplication
GUI is NOT involved in this server application.
Basically I want to change the default behaviour(kill the application) when CTRL+C is pressed from the command prompt, or 'kill pid' UNIX command is issued to the kill the process. I need to do quite some cleanup work(possibly create a new thread to do the cleanup) before the application exits.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to catch SIGINT. I found the following on the net:
http://www.stanford.edu/~mgritter/java-SIGINT.html
http://www.interstice.com/~kevinh/projects/javasignals/
They use JNI and only work on unix. Let me know if you come across any Windoz version.
regards,
Raj
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a shutdown handler -- but a shutdown handler than just keeps things running might possibly cause the virtual machine to get mad.
 
reply
    Bookmark Topic Watch Topic
  • New Topic