我如何“拦截”在CLI应用程序中Ctrl + C? [英] How can I "intercept" Ctrl+C in a CLI application?

查看:206
本文介绍了我如何“拦截”在CLI应用程序中Ctrl + C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在CLI(命令行界面)中拦截 )Java应用程序?

How can I intercept Ctrl+C (which normally would kill the process) in a CLI (command line interface) Java application?

是否存在多平台解决方案(Linux,Solaris和Windows)?

Does a multi-platform solution exist (Linux, Solaris, Windows)?

'm使用控制台 readLine(),但如果必要,我可以使用一些其他方法
从标准输入读取字符。

I'm using Console's readLine(), but if necessary, I could use some other method to read characters from standard input.

推荐答案

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() { /*
       my shutdown code here
    */ }
 });

这应该能够拦截信号,但只能作为一个中间步骤,JVM完全关闭本身

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.

您需要使用 SignalHandler

这篇关于我如何“拦截”在CLI应用程序中Ctrl + C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆