刚刚找到这篇文章(并找到了我也在下面粘贴的代码):
java runtime.getruntime() getting output from executing a command line program
我的问题是,如何终止该进程?看起来代码在 while 循环中阻塞。我尝试了几种选择,例如使用 boolean 值、在单独的线程中运行所有代码等等,但没有成功。 我只想启动一个 Android 模拟器并在需要时终止它。
Runtime rt = Runtime.getRuntime();
String[] commands = {"emulator", "-avd", "jenkins",
"-scale", "96dpi", "-dpi-device", "100"};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
最佳答案
好的。
使用下面的代码获取当前正在运行的线程或进程的进程 ID。
String processName =java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
String ProcessID = processName.split("@")[0];//Process Id
使用该进程 ID 来终止 CPU 中的该进程。
我认为为此目的,您可能希望在 While 循环中编写任何其他触发器或任何条件。
关于java - 在 Java 中启动进程后如何退出 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30319218/