Bober02
Bober02

Reputation: 15341

ProcessBuilder - hanging on the readLine() method

Running the following simple program:

    ProcessBuilder pb = new ProcessBuilder("powershell.exe", "-Command", "dir");
    Process p = pb.start();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while((line = bufferedReader.readLine()) != null){
        System.out.println(line);
    }

    System.out.println("Exit");

Never reaches the "Exit" println - just hangs infinitely on the readLine() method. I understand this is (most probably) caused by the fact that powershell does not output \n in the last line and readLine is not sure whether the end has been reached or not. Is there a way to get over this issue and read the input stream correctly? BTW. inheritIO method on processbuilder resulted in the same issue...

UPDATE

This:

 ProcessBuilder pb = new ProcessBuilder("powershell.exe", "-Command", "dir");
        pb.redirectErrorStream(true);
        Process p = pb.start();
//        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
//        String line;
//        while((line = bufferedReader.readLine()) != null){
//            System.out.println(line);
//        }
    p.waitFor();
    System.out.println("Exit");

Also hangs infinitely...

Upvotes: 2

Views: 2804

Answers (3)

AnAmuser
AnAmuser

Reputation: 1895

I had a problem with the processbuilder hanging on readline()

Specifically, windows 10, running a cmd.exe command that starts a bash and runs a script.

The problem was fixed by closing the input of the process:

     Process process = pb.start();
     process.getOutputStream().flush();
     process.getOutputStream().close();

Upvotes: 1

chronodekar
chronodekar

Reputation: 2746

Not sure what the problem is. I tried making a new project with your code (added in a few try-catch and print statements), and it works for me. Here is what I used;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class temp {

    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("powershell.exe", "-Command", "dir");
        Process p;
        try {
            p = pb.start();
        } catch (IOException e) {
            System.out.println("Failed to start powershell");
            return;
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        System.out.println("Begin!");
        try {
            while((line = bufferedReader.readLine()) != null){
                System.out.println("reading...");
                System.out.println(line);
            }
        } catch (IOException e) {
            System.out.println("Failed to read line");
            return;
        }

        System.out.println("Exit");

    }

}

And here is the resulting console output,

Begin! reading...

reading...

reading...
    Directory: C:\Users\AbrahamV\workspace\201_stackoverflow reading...

reading...

reading... Mode                LastWriteTime     Length Name           reading...
----                -------------     ------ ----                                                                       reading... d----        12/10/2013   9:29 PM            bin            reading... d----        12/10/2013   9:27 PM            src            reading...
-a---        12/10/2013   9:27 PM        232 .classpath                                                                 reading...
-a---        12/10/2013   9:27 PM        393 .project                                                                   reading...

reading...

Exit

The output wasn't instantaneous. Took a few moments before anything was printed out.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

Powershell isn't ending. I would probably use Java to list a directory, but this should work with your example.

ProcessBuilder pb = new ProcessBuilder("dir");

Upvotes: 1

Related Questions