Java程序执行命令要花费很长时间
我已经阅读了许多示例,并最终使用以下代码从Java程序内部执行了命令行命令。
public static void executeCommand(final String command) throws IOException,
InterruptedException {
System.out.println("Executing command " + command);
final Runtime r = Runtime.getRuntime();
final Process p = r.exec(command);
System.out.println("waiting for the process");
p.waitFor();
System.out.println("waiting done");
try (final BufferedReader b = new BufferedReader(new InputStreamReader(
p.getInputStream()))) {
String line;
while ((line = b.readLine()) != null) {
System.out.println(line);
}
}
}
我已经用一个简单的ls命令测试了它,并且工作正常。当我尝试运行另一个命令时,它将永远耗费时间(保持运行25分钟,但尚未停止)。
当我在命令行上执行tabix命令时,我得到以下统计信息
4.173u 0.012s 0:04.22 99.0%0 + 0k 0 + 0io 0pf + 0w
因此,它应该很快完成。
该命令是
时间标签文件pos1 pos2 … pos190> / dev / null
问题可能是tabix命令包含> /dev/null
在末尾吗?如果不是,什么原因可能导致此问题?
-
您需要 先将 阅读器附加到该流程上, 然后才能 调用它
waitFor
。没有它,它 可能会 填满分配的输出缓冲区,然后阻塞-
但仅对于大输出,小(例如测试)输出似乎很好。public static void executeCommand(final String command) throws IOException, InterruptedException { System.out.println("Executing command " + command); // Make me a Runtime. final Runtime r = Runtime.getRuntime(); // Start the command process. final Process p = r.exec(command); // Pipe it's output to System.out. try (final BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()))) { String line; while ((line = b.readLine()) != null) { System.out.println(line); } } // Do this AFTER you've piped all the output from the process to System.out System.out.println("waiting for the process"); p.waitFor(); System.out.println("waiting done"); }
-
SSMS速度快而应用程序速度慢-为什么要花费这么长时间才能填充此DataSet?
2021-04-09 关注 0 浏览55 1答案
-
不宜团体施测,需要花费大量时间的测试是( )。
2021-05-07 关注 0 浏览42 1答案
-
从Java程序执行ADB命令
2021-01-30 关注 0 浏览81 1答案
-
在Java程序中执行PowerShell命令
2021-01-31 关注 0 浏览81 1答案
-
在Java程序中执行bash命令
2021-01-29 关注 0 浏览105 1答案
-
Flutter 花费了很长时间才能连接到浏览器
2021-01-31 关注 0 浏览178 1答案
-
如何从Java程序执行“ javac和java命令”?
2021-01-29 关注 0 浏览89 1答案
-
Java执行命令行程序'find'返回错误
2021-01-29 关注 0 浏览102 1答案
-
在Node.js中循环使用findOne会花费很长时间
2021-01-31 关注 0 浏览91 1答案
-
Java Runtime.getRuntime():从执行命令行程序获取输出
2021-02-02 关注 0 浏览239 1答案