ProcessBuilder redirected to standard output

前端 未结 2 1074
Happy的楠姐
Happy的楠姐 2021-02-18 14:56

I would like to redirect a java process output towards the standard output of the parent java process.

Using the ProcessBuilder class as follows:

public          


        
相关标签:
2条回答
  • 2021-02-18 15:28

    You did miss a key piece, you actually need to start your process and wait for your output. I believe this will work,

    processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    // Start the process.
    try {
      Process p = processBuilder.start();
      // wait for termination.
      p.waitFor();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    
    0 讨论(0)
  • 2021-02-18 15:49

    Try ProcessBuilder.inheritIO() to use the same I/O as the current Java process. Plus you can daisy chain the methods:

    ProcessBuilder pb = new ProcessBuilder("cmd")
        .inheritIO()
        .directory(new File("C:"));
    pb.start();
    
    0 讨论(0)
提交回复
热议问题