Java进程构建器手动管道

问题描述 投票:0回答:1

grep ". Cmd1 and Cmd2 are the commands on either side of the grep ". Cmd1 and Cmd2 are the commands on ...

 private void pipe(String Cmd1, String Cmd2, String[] baseCommand1)
    {
        if(System.getProperty("os.name").toLowerCase().startsWith("windows"))
        {
            if(System.getProperty("os.name").toLowerCase().startsWith("windows"))
            {
                baseCommand1[0] = "cmd.exe";
                baseCommand1[1] = "/c";
            }
            else
            {
                baseCommand1[0] = "sh";
                baseCommand1[1] = "-c";
            }
        }

        try {
            String[] p1Cmd = { baseCommand1[0], baseCommand1[1], Cmd1 };
            String[] p2Cmd = { baseCommand1[0], baseCommand1[1], Cmd2 };

            ProcessBuilder pb1 = new ProcessBuilder(p1Cmd);
            ProcessBuilder pb2 = new ProcessBuilder(p2Cmd);          
            pb1.redirectInput(ProcessBuilder.Redirect.INHERIT);
            pb1.redirectOutput(pb2.redirectInput());
            pb2.redirectOutput(ProcessBuilder.Redirect.INHERIT);


            Process p1 = pb1.start();
            Process p2 = pb2.start();

            java.io.InputStream in = p1.getInputStream();
            java.io.OutputStream out = p2.getOutputStream();

            int c;
            while ((c = in.read()) != -1) {
                System.out.println(c);
                    out.write(c);
            }

            out.flush();
            out.close();


            if(!Cmd2.contains("&"))
            {
                p1.waitFor();
                p2.waitFor();
            }

        }
        catch (Exception ex) {
            System.out.print(ex);
        }
    }




Does your sub-process write a lot of data? A common problem with using the Process class via Runtime.exec or ProcessBuilder is that you can block in the Java application if you do not consume large standard output (or error) streams generated by the sub-process.
java pipe processbuilder
1个回答
0
投票

It may be that your cmd 我一直在测试 "两个进程之间的dir O "命令,但似乎得不到任何输出。我加入了这个函数。我一直在测试 "dir O "命令,但似乎得不到任何输出。

© www.soinside.com 2019 - 2024. All rights reserved.