[英]Java exec. Is it possible to get force flush?
I am new in java, so my question can be muddled. 我是Java的新手,所以我的问题可能会混乱。
I tried to run some process from java. 我试图从Java运行一些进程。 It is
xmr-stack
miner. 这是
xmr-stack
矿工。 I use code like that: 我使用这样的代码:
package com.company;
import java.io.*;
public class Main {
public static void main(String[] argv) throws Exception {
try {
String line;
Process p = Runtime.getRuntime().exec( "D:\\xmr-stak.exe " +
/* some arguments */ );
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()) );
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
catch (Exception e) {
// ...
}
}
}
It works perfect for common things. 它适用于普通事物。
But I faced an issue that I have no output after some point in xmr-stak
. 但是我遇到了一个问题,就是在
xmr-stak
某个点之后我没有输出。 As far as I understand at some point this app create child process. 据我了解,此应用程序有时会创建子进程。 And I didn't see output produced by this child process.
而且我没有看到此子进程产生的输出。
But after very long time working (10+ minutes) I got my output for all this time. 但是经过很长时间的工作(超过10分钟),我得到了这段时间的输出。 It is looks like some output buffer was flashed after overflow.
看起来有些输出缓冲区在溢出后被刷新了。 Now I want to understand how to get required output more often in java.
现在,我想了解如何在Java中更频繁地获取所需的输出。
From other side I wrote same logic in c++ (Based on this question SO arswer ) And I got my output in time. 另一方面,我用c ++编写了相同的逻辑(基于这个问题,所以很了解 ),我及时得到了输出。
Runtime.exec is obsolete. Runtime.exec已过时。 You can replace your entire program with a few lines that use ProcessBuilder :
您可以用几行使用ProcessBuilder替换整个程序:
ProcessBuilder builder = new ProcessBuilder("D:\\xml-stak.exe",
arg1, arg2, arg3);
builder.inheritIO();
Process p = builder.start();
p.waitFor();
You don't need to read the process's output (and therefore don't have to worry about buffering), because inheritIO() makes that output appear in your Java program's output. 您不需要读取进程的输出(因此不必担心缓冲),因为InheritIO()使该输出出现在Java程序的输出中。
You also don't need to catch any exceptions, since your main
method already has throws Exception
. 您还不需要捕获任何异常,因为您的
main
方法已经throws Exception
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.