如何将中断键序列发送到 Java 进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/912889/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:20:12  来源:igfitidea点击:

How to send interrupt key sequence to a Java Process?

javaprocessstreambreakcopy-paste

提问by Ben Vitale

I've got a handle to a Java Process instance and its associated streams. It's a console program. I'd like to simulate a break sequence. On Windows this is Ctrl-C. Is this possible without natives?

我有一个 Java Process 实例及其关联流的句柄。这是一个控制台程序。我想模拟一个中断序列。在 Windows 上,这是 Ctrl-C。如果没有当地人,这可能吗?

The reason for doing this: the console program is a command-line console itself, controlling a virtual machine for another language. The user can run another program from this console program. While a program is running, the break sequence will interrupt execution of the underlying program and cause the console program to go into debug mode.

这样做的原因:控制台程序本身就是一个命令行控制台,用于控制另一种语言的虚拟机。用户可以从这个控制台程序运行另一个程序。当程序运行时,中断序列将中断底层程序的执行并使控制台程序进入调试模式。

We are Java-wrapping this console debugger so that it can be driven via an IDE (Eclipse). Sending the break sequence will be the equivalent of pressing "suspend" in the Eclipse debugger.

我们正在 Java 包装这个控制台调试器,以便它可以通过 IDE (Eclipse) 驱动。发送中断序列相当于在 Eclipse 调试器中按“挂起”。

回答by Eddie

You probably want to look into SendSignal. This will send a ctrl-Break to a Java Process. I assume your goal is to cause a stack dump, which a CTRL-C (signal 3 aka SIGQUIT) will do for a running Java process?

您可能想查看SendSignal。这将向 Java 进程发送 ctrl-Break。我假设您的目标是导致堆栈转储,CTRL-C(信号 3 又名 SIGQUIT)将为正在运行的 Java 进程做些什么?