作者Banciao (板桥是个好地方)
看板java
标题Re: [问题] 使用runtime呼叫bat档
时间Fri Dec 17 19:51:48 2010
※ 引述《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