If you have a console program and want to use the Ctrl+C for closing program resources (like the notorious JBoss shutdown sequence), you must use a ShutdownHook:
public class TestCtrlC {
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println ("Called at shutdown.");
}
});
System.out.println ("Hit Ctrl+C (not Ctrl+Break!)");
System.out.println ("Trying to sleep 10 seconds...");
try { Thread.sleep (10000); } catch (InterruptedException ex) {}
System.out.println ("Sleep end");
}
}
If you want to trap (handle) the Ctrl+C and other signals, use the undocumented, Sun JVM-specific, sun.misc.Signal and sun.misc.SignalHandler classes. You can get the full source in the SCSL Source Code.
package sun.misc;
/**
* This class provides ANSI/ISO C signal support. A Java program can register
* signal handlers for the current process. There are two restrictions:
* <ul>
* <li>
* Java code cannot register a handler for signals that are already used
* by the Java VM implementation. The <code>Signal.handle</code>
* function raises an <code>IllegalArgumentException</code> if such an attempt
* is made.
* <li>
* When <code>Signal.handle</code> is called, the VM internally registers a
* special C signal handler. There is no way to force the Java signal handler
* to run synchronously before the C signal handler returns. Instead, when the
* VM receives a signal, the special C signal handler creates a new thread
* (at priority <code>Thread.MAX_PRIORITY</code>) to
* run the registered Java signal handler. The C signal handler immediately
* returns. Note that because the Java signal handler runs in a newly created
* thread, it may not actually be executed until some time after the C signal
* handler returns.
* </ul>
* <p>
* Signal objects are created based on their names. For example:
* <blockquote><pre>
* new Signal("INT");
* </blockquote></pre>
* constructs a signal object corresponding to <code>SIGINT</code>, which is
* typically produced when the user presses <code>Ctrl-C</code> at the command line.
* The <code>Signal</code> constructor throws <code>IllegalArgumentException</code>
* when it is passed an unknown signal.
* <p>
* This is an example of how Java code handles <code>SIGINT</code>:
* <blockquote><pre>
* SignalHandler handler = new SignalHandler () {
* public void handle(Signal sig) {
* ... // handle SIGINT
* }
* };
* Signal.handle(new Signal("INT"), handler);
* </blockquote></pre>
*
* @author Sheng Liang
* @author Bill Shannon
* @version @(#)Signal.java 1.8 03/01/23
* @see sun.misc.SignalHandler
* @since JDK1.2
*/
public final class Signal {
/* Returns the signal number */
public int getNumber() {
/**
* Returns the signal name.
*
* @return the name of the signal.
* @see sun.misc.Signal#Signal(String name)
*/
public String getName() {
/**
* Compares the equality of two <code>Signal</code> objects.
*
* @param other the object to compare with.
* @return whether two <code>Signal</code> objects are equal.
*/
public boolean equals(Object other) {
/**
* Returns a hashcode for this Signal.
*
* @return a hash code value for this object.
*/
public int hashCode() {
/**
* Returns a string representation of this signal. For example, "SIGINT"
* for an object constructed using <code>new Signal ("INT")</code>.
*
* @return a string representation of the signal
*/
public String toString() {
/**
* Constructs a signal from its name.
*
* @param name the name of the signal.
* @exception IllegalArgumentException unknown signal
* @see sun.misc.Signal#getName()
*/
public Signal(String name) {
/**
* Registers a signal handler.
*
* @param sig a signal
* @param handler the handler to be registered with the given signal.
* @result the old handler
* @exception IllegalArgumentException the signal is in use by the VM
* @see sun.misc.Signal#raise(Signal sig)
* @see sun.misc.SignalHandler
* @see sun.misc.SignalHandler#SIG_DFL
* @see sun.misc.SignalHandler#SIG_IGN
*/
public static synchronized SignalHandler handle(Signal sig,
SignalHandler handler)
/**
* Raises a signal in the current process.
*
* @param sig a signal
* @see sun.misc.Signal#handle(Signal sig, SignalHandler handler)
*/
public static void raise(Signal sig) throws IllegalArgumentException {
}
/**
* This is the signal handler interface expected in <code>Signal.handle</code>.
*
* @author Sheng Liang
* @author Bill Shannon
* @version @(#)SignalHandler.java 1.7 03/01/23
* @see sun.misc.Signal
* @since JDK1.2
*/
public interface SignalHandler {
/**
* The default signal handler
*/
public static final SignalHandler SIG_DFL = new NativeSignalHandler(0);
/**
* Ignore the signal
*/
public static final SignalHandler SIG_IGN = new NativeSignalHandler(1);
/**
* Handle the given signal
*
* @param sig a signal object
*/
public void handle(Signal sig);
}