我正在从我的 Java 应用程序 (8u11) 中启动一个外部应用程序。但是,应用程序在 Windows XP 和 Windows 7 下使用标准沙漏/微调器对 UI 输入无响应。
我已将这个问题缩小到是否使用 Process.waitFor()。如果我调用它,我会看到问题,如果我不调用它,它会正常工作。如果我随后退出 Java 应用程序,应用程序也会解冻。
我的问题是为什么会这样——调用 waitFor() 怎么可能影响子进程的内部运行?我怎样才能避免这个问题?
有问题的应用程序是 LinPhone.exe,但我不认为该问题是特定于应用程序的——它必须有一些通用的方式来处理标准 IO 等,而我通过调用 waitFor() 来干扰这种方式。
我需要使用 Process.waitFor() 以便我可以跟踪应用程序何时退出。
我已将问题简化为此 SCCEE。
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class LinphoneTest {
public static void main(String[] args) throws IOException,
InterruptedException {
String phoneAppPath = "C:\\Program Files\\Linphone\\bin\\linphone.exe";
ProcessBuilder processBuilder = new ProcessBuilder(phoneAppPath);
// move up from bin/linephone.exe
File workingDir = new File(phoneAppPath).getParentFile()
.getParentFile();
processBuilder.directory(workingDir);
processBuilder.redirectErrorStream();
Process process = processBuilder.start();
final BufferedReader stdout = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = null;
try {
while (((line = stdout.readLine()) != null)) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
new Thread(() -> {
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "process wait").start();
Thread.sleep(Long.MAX_VALUE);
}
}