-1
我在我的Java應用程序(Runtime)中創建一個.bat文件,然後運行.bat文件並使用waitFor()方法來等待終止.bat文件,但進程無需等待終止.bat文件。
進程沒有等待終止蝙蝠文件
代碼:
public boolean restoreDepot() {
try {
InputStreamReader _input = new
InputStreamReader(getClass().getResourceAsStream("/Depot/DBMakaseb.sql"));
BufferedReader _in = new BufferedReader(_input);
String _currentpath = (new File(".")).getCanonicalPath() + File.separator + "DBDepot.sql";
BufferedWriter _out = new BufferedWriter(new FileWriter(_currentpath));
while (_in.ready()) {
_out.write(_in.readLine());
_out.newLine();
}
_in.close();
_out.close();
String _comCur = "set cur=%cd%";
String _comCD = "Cd /d %PROGRAMFILES%\\MySQL\\MySQL Server 5.5\\bin";
String _comando = "mysql -u root -pm117988m < %cur%" + "\\DBDepot.sql";
String _comExit = "exit";
BufferedWriter _out1 = new BufferedWriter(new FileWriter("restore.bat"));
_out1.write(_comCur);
_out1.newLine();
_out1.write(_comCD);
_out1.newLine();
_out1.write(_comando);
_out1.newLine();
_out1.write(_comExit);
_out1.close();
Process _p = Runtime.getRuntime().exec("cmd /C start restore.bat");
int _res = _p.waitFor();
while (!(((new File(_currentpath)).delete()) && ((new File((new File(".")).getCanonicalPath() + File.separator + "restore.bat")).delete()))) {
}
if (_res != 0) {
return false;
}
return true;
} catch (Exception err) {
System.out.println(err);
}
return false;
}
請幫幫我!
答案就在這裏:http://stackoverflow.com/questions/2448402/run-bat-file-in-java-and-wait-2 – 2012-02-18 18:43:55