在Java应用程序中:
currentProcess = Runtime.getRuntime().exec("MyWindowsApp.exe");
...
currentProcess.destroy();
Run Code Online (Sandbox Code Playgroud)
调用destroy只会杀死进程,不允许任何用户清理或退出代码运行.是否可以向进程发送WM_CLOSE消息或类似消息?
您可以使用Process.getOutputStream将消息发送到应用程序的标准输入,例如:
PrintStream ps = new PrintStream(currentProcess.getOutputStream());
ps.println("please_shutdown");
ps.close();
Run Code Online (Sandbox Code Playgroud)
当然,这意味着您必须设法在 Windows 应用程序中监听 stdin。