看板 java 關於我們 聯絡資訊
※ 引述《fiend1212 (YOOOOOOOOOOOOOOOOOOOOOO)》之銘言: : 不好意思請問各位大大 : 小弟最近使用Runtime呼叫bat檔去執行另一支JAVA程式 : 然後主程式要等這支程式跑完才繼續執行 : 但卻一直都等不到他結束...程式就卡在那 : 以下是我呼叫的程式碼 : try{ : Runtime rt = Runtime.getRuntime(); : Process ps = rt.exec("C:/Users/admin/JAVA/Login.bat"); : ps.waitFor(); : } : catch(Exception e){e.printStackTrace();} : 因為另一支JAVA執行完會產生一個檔案 : 若沒有ps.waitFor();那個檔案還是沒有產生 : 可是手動執行那個bat檔就可以正常執行 : 請問有可能是什麼原因嗎? waitFor() 的說明裡面就有寫啦 causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits. 簡單來說就是你想執行 Login.bat ,若 1.有呼叫 waitFor() 它會乖乖等 Login.bat 跑完才結束,所以你會有正確執行 Login.bat 的結果 2.沒有呼叫 waitFor() 它會直接往下執行,可是後面沒有程式了,也就是直接結束掉,至於 Login.bat 有沒有被正確執行並不是它要顧慮的事,運氣好就有,運氣不好就沒有 所以沒呼叫 waitFor() 就得不到正確執行 Login.bat 的結果是合理的 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.30.33