0

I am new in java, so my question can be muddled.

I tried to run some process from java. It is xmr-stack miner. 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. 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. 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.

From other side I wrote same logic in c++ (Based on this question SO arswer) And I got my output in time.

Stepan Loginov
  • 1,667
  • 4
  • 22
  • 49
  • A child process which is writing to the error stream will wait until the buffer has enough space to write some more. If you don't read the error stream the program will stop if it fill. – Peter Lawrey Jul 22 '18 at 19:35

1 Answers1

0

Runtime.exec is obsolete. You can replace your entire program with a few lines that use 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.

You also don’t need to catch any exceptions, since your main method already has throws Exception.

VGR
  • 40,506
  • 4
  • 48
  • 63