gpt4 book ai didi

java - Linux Java 进程重定向

转载 作者:太空宇宙 更新时间:2023-11-04 03:47:20 26 4
gpt4 key购买 nike

我的代码在 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 不包含。在这种情况下我可以使用任何等效的东西吗?

谢谢

最佳答案

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

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));

关于java - Linux Java 进程重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27688119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com