我正在尝试在 MySQL 数据库中导入大量 .dmp 文件,由于有超过 250 个文件需要导入,我编写了一个应用程序来自动执行 250 个文件+ DOS 命令。它的代码:
String baseCommand = "cmd /c MySQL -h localhost -u root amateurstable < ";
Process p = Runtime.getRuntime().exec(baseCommand + filePath);
它确实执行了它应该执行的命令。问题是一些 .dmp 文件大于 100MB,但上面的代码并没有等到命令执行完成。
当它为一个大文件执行导入命令时,它不会等到导入结束并立即执行下一个命令。这在计算机的响应能力方面引起了很多麻烦。
问题是如何让它等到命令执行完成?
最佳答案
Runtime.exec 返回 Process具有 waitFor() 方法的对象。
waitFor() causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.
因为你已经有了那个 Process 对象,你可以添加对 waitFor() 的调用
Process p = Runtime.getRuntime().exec(baseCommand + filePath);
p.waitFor();
关于java - 等到 DOS 命令执行完成 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8165187/