流程重定向Linux Java

时间:2014-12-29 11:37:19

标签: java linux redirect

我有这个代码在Windows中完美运行:

 //We create the process and pipe it out and in
                    if (isWindows(OS)) {
                        shell="cmd.exe";
                    }
                    else{
                        shell="/bin/sh";
                    }

                    ProcessBuilder processBuilder = new ProcessBuilder(shell).redirectInput(Redirect.PIPE)
                            .redirectOutput(Redirect.PIPE).redirectError(Redirect.PIPE);
                    Process process = processBuilder.start();
                    final BufferedReader ProcessIN = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    final BufferedReader ProcessERROR = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                    final BufferedWriter ProcessOUT = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));

问题是我希望它也可以在linux / MacOS上工作而且它不能编译,因为库java.lang.ProcessBuilder.Redirect不包括。 在这种情况下,我可以使用任何等价物吗?

谢谢

1 个答案:

答案 0 :(得分:0)

找到了如何让它适用于两种操作系统:

Process process = Runtime.getRuntime ().exec (shell);
                    OutputStream stdin = process.getOutputStream ();
                    InputStream stderr = process.getErrorStream ();
                    InputStream stdout = process.getInputStream ();

                    final BufferedReader ProcessIN = new BufferedReader (new InputStreamReader(stdout));
                    final BufferedReader ProcessERROR = new BufferedReader (new InputStreamReader(stderr));
                    final BufferedWriter ProcessOUT = new BufferedWriter(new OutputStreamWriter(stdin));