How to make pipes work with Runtime.exec()?

后端 未结 4 1451
日久生厌
日久生厌 2020-11-22 03:35

Consider the following code:

String commandf = \"ls /etc | grep release\";

try {

    // Execute the command and wait for it to complete
    Process child =         


        
4条回答
  •  逝去的感伤
    2020-11-22 04:13

    Write a script, and execute the script instead of separate commands.

    Pipe is a part of the shell, so you can also do something like this:

    String[] cmd = {
    "/bin/sh",
    "-c",
    "ls /etc | grep release"
    };
    
    Process p = Runtime.getRuntime().exec(cmd);
    

提交回复
热议问题