Show output while execute long-time running command Java without waiting RuntimeExec

1.3k views Asked by At

I have command that takes about 4 minutes to complete execution. While executing the command from windows command prompt, it shows timing information and continuous output.

I want to show that output while running the command from my Java code. Normal execution waits until the command exit and get the output. I want to get the output while running without waiting the command to finish.

Here is my code:

try{
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(args);
        String line="";
        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
         while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            input.close();


        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        errorReader.lines().forEach(System.out::println);

        process.waitFor();

        System.out.println("---------------------------------------");
        }
        catch (Exception x){
            System.out.println(x.getMessage());
        }

Problem with this code is that it wits until the command ends then print all output at once.

2

There are 2 answers

0
VGR On

The easiest solution is to stop using Runtime.exec, and use the more modern ProcessBuilder class instead. Its inheritIO() method will do exactly what you want:

ProcessBuilder builder = new ProcessBuilder(args);
builder.inheritIO();
Process process = builder.start();

You can’t read all of a process’s standard output or standard error at once. They can appear concurrently, and failing to read either one might or might not cause a process to hang. inheritIO() solves that problem.

0
MSaudi On

For a reason I don't know about, The command I use, which is ffmpeg -i, to produce some resolutions of a video. It writes the output messages to the ErrorStream not OutputStream. When I printed the error stream first, I can see the output.

        try{
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(args);

        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        BufferedReader lineReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
         while ((line = err.readLine()) != null) {
                System.out.println(line);
            }
           err.close();

        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
         while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            input.close();  




        process.waitFor();

        System.out.println("---------------------------------------");
    }
    catch (Exception x){
        System.out.println(x.getMessage());
    }