Java 如何在 CLI 应用程序中“拦截”Ctrl+C?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1216172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I "intercept" Ctrl+C in a CLI application?
提问by ivan_ivanovich_ivanoff
How can I interceptCtrl+C(which normally would kill the process) in a CLI (command line interface) Java application?
如何在 CLI(命令行界面)Java 应用程序中拦截Ctrl+ C(通常会终止进程)?
Does a multi-platform solution exist (Linux, Solaris, Windows)?
是否存在多平台解决方案(Linux、Solaris、Windows)?
I'm using Console
's readLine()
, but if necessary, I could use some other method
to read characters from standard input.
我正在使用Console
's readLine()
,但如有必要,我可以使用其他一些方法从标准输入中读取字符。
采纳答案by VonC
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 before the JVM completely shutdowns itself, so it may not be what you are looking after.
这应该能够拦截信号,但只能作为 JVM 完全关闭自身之前的中间步骤,因此它可能不是您所关注的。
You need to use a SignalHandler
(sun.misc.SignalHandler
) to intercept the SIGINT
signal triggered by a Ctrl+C(on Unix as well as on Windows).
See this article(pdf, page 8 and 9).
您需要使用SignalHandler
( sun.misc.SignalHandler
) 来拦截SIGINT
由Ctrl+触发的信号C(在 Unix 和 Windows 上)。
请参阅这篇文章(pdf,第 8 页和第 9 页)。
回答by akf
I am assuming you want to shutdown gracefully, and not do short circuit the shutdown process. If my assumption is correct, then you should look at Shutdown Hooks.
我假设您想正常关机,而不是使关机过程短路。如果我的假设是正确的,那么您应该查看Shutdown Hooks。
回答by Amber
In order to be able to handle Ctrl+Cwithout shutting down for some reason, you'll need to use some form of signal handling (since the Ctrl+Cinput isn't actually passed directly to your application, but instead is handled by the OS which generates a SIGINT that is then passed to Java.
为了能够处理Ctrl+C而不出于某种原因关闭,您需要使用某种形式的信号处理(因为Ctrl+C输入实际上并未直接传递给您的应用程序,而是由生成然后传递给 Java 的 SIGINT。
See http://www.oracle.com/technetwork/java/javase/signals-139944.htmlfor details on signal handling.
有关信号处理的详细信息,请参阅http://www.oracle.com/technetwork/java/javase/signals-139944.html。
(If you're just wanting to gracefully shutdown, akf's answer will suffice.)
(如果您只是想正常关机,akf 的回答就足够了。)
回答by Henning
Some links about how to handle SIGTERM - that is the signal the program is getting on the OS side:
关于如何处理 SIGTERM 的一些链接 - 这是程序在操作系统端获得的信号:
http://blog.webinf.info/2008/08/intercepting-sigterm.html
http://blog.webinf.info/2008/08/intercepting-sigterm.html
http://java.sun.com/javase/6/webnotes/trouble/TSG-VM/html/signals.html
http://java.sun.com/javase/6/webnotes/trouble/TSG-VM/html/signals.html
http://www.ibm.com/developerworks/java/library/i-signalhandling/
http://www.ibm.com/developerworks/java/library/i-signalhandling/
That should work on POSIX operating systems - that is, Mac and Unix should work, on windows I'm not sure, I remember hearing it is also POSIX compatible to some extent, but might varty a lot with different versions.
这应该适用于 POSIX 操作系统——也就是说,Mac 和 Unix 应该可以工作,在 Windows 上我不确定,我记得听说它在某种程度上也兼容 POSIX,但可能会因不同版本而异。