我正在啓動一個服務(進程),下面的代碼。我的問題是:從阻止進程中優雅退出
- 我需要讀取過程的輸出,以確保它得到啓動
- ,如果它得到開始,我回來,一切都很好
- ,如果它不上手無論出於何種原因,在同時將永遠阻止的過程只是掛起,而不輸出任何東西
任何想法我如何能正常退出的方法,如果我沒有得到期望的字符串?
PS:我可以用一個未來超時做它,但還是覺得有可能是一個更好的辦法。
public boolean startService() {
try {
ProcessBuilder pb = new ProcessBuilder("service.exe");
pb.directory(new File("C:/serviceFolder/"));
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.toLowerCase().contains("started")) {
return true;
}
}
return false; //I never get there when it fails
} catch (IOException e) {
throw new RuntimeException("Could not start the service.exe process", e);
}
}
一個'Future'一個超時*爲*更好的辦法 – Bohemian