How to send interrupt key sequence to a Java Proce

2019-03-15 20:20发布

问题:

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?

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.

回答1:

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?