0
//Credit to this post as much of the code was sourced from it http://stackoverflow.com/a/37629840
public class ApplicationUtilities {
// http://stackoverflow.com/a/19005828/3764804
private static boolean isProcessRunning(String processName) throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder("tasklist.exe");
Process process = processBuilder.start();
String tasksList = toString(process.getInputStream());
return tasksList.contains(processName);
}
// http://stackoverflow.com/a/5445161/3764804
private static String toString(InputStream inputStream) {
Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A");
String string = scanner.hasNext() ? scanner.next() : "";
scanner.close();
return string;
}
}
此代碼是爲我工作,昨晚但現在當我嘗試添加的do while循環的isProcessRunning
一段代碼將只返回值false。 有沒有什麼我做錯了我的一端,因爲這些特定的程序正在返回一個真正的值運行之前,我在程序的另一部分添加了do while循環。檢查,如果一個文件被運行,另一種是不
'taskList'的內容是什麼?你嘗試了一個簡單的'System.out.println(tasksList)'嗎? – MikaelF