2017-02-09 68 views
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循環。檢查,如果一個文件被運行,另一種是不

+0

'taskList'的內容是什麼?你嘗試了一個簡單的'System.out.println(tasksList)'嗎? – MikaelF

回答

0

沒有意識只是意識到我寫錯了程序錯誤的變量,並保持該程序相同(它沒有運行),並將其他變量更改爲正在運行的程序,所以它返回false因爲程序確實沒有運行

相關問題