How can I “intercept” Ctrl+C in a CLI application?
问题 How can I intercept Ctrl + C (which normally would kill the process) in a CLI (command line interface) Java application? Does a multi-platform solution exist (Linux, Solaris, Windows)? I\'m using Console \'s readLine() , but if necessary, I could use some other method to read characters from standard input. 回答1: Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { /* my shutdown code here */ } }); This should be able to intercept the signal, but only as an intermediate step