简体   繁体   English

Java,运行外部程序

[英]Java, running external programs

I am trying to write a chatbot. 我正在尝试写一个聊天机器人。 I am still in my startings, but I do have one question. 我仍处于起步阶段,但是我有一个问题。

Process proc = Runtime.getRuntime().exec("notepad.exe");

This actually makes it impossible to quit my program, unless I quit the notepad. 实际上,除非我退出记事本,否则就无法退出程序。 Is it possible to quit the Java program before this notepad has ended? 是否可以在此记事本结束之前退出Java程序?

您可以通过程序中的单独线程运行外部程序,并在主线程中继续执行程序逻辑,即, 多线程应用程序是一个很好的解决方案。

First destroy the subprocess : 首先销毁子流程:

Process proc = Runtime.getRuntime().exec("notepad.exe");
proc.destroy();

If this doesn't work, get the process id of the process and run a different subprocess to run kill <pid> 如果这不起作用,请获取该进程的进程ID,然后运行另一个子进程来运行kill <pid>

then exit java with System.exit(0); 然后使用System.exit(0);退出java System.exit(0); or normally. 或通常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM