ProcessBuilder gets stuck after getting an error

倖福魔咒の 提交于 2019-12-06 13:21:36

You need to make sure you're also dealing with stderr, and you should be dealing with both streams in separate threads.

Read this and make sure you follow all the advice.

Edit: Looking at the code you've written, you seem to have reproduced code from this precise article. In fact, it looks like listing 4.3 (MediocreExecJavac.java).

Add redirectErrorStream(true) before you .start() and read getInputStream(). This will read stderr and merge that with stdout into one response which can be read with getInputStream().

    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.redirectErrorStream(true);
    final Process process = processBuilder.start();

    InputStream stderr = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(stderr);
    BufferedReader br = new BufferedReader(isr);
    String line = null;


    while ((line = br.readLine()) != null) {
        System.out.println(line);

    }
    process.waitFor();
    System.out.println("Waiting ...");

    System.out.println("Returned Value :" + process.exitValue());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!