說我有這樣的功能:檢索的ProcessBuilder
public String runCommand(parameters, boolean interactive)
{
Process p = null;
// some code
try
{
final ProcessBuilder pb = new ProcessBuilder(my_command);
pb.inheritIO(); // So the output is displayed on the console
p = pb.start();
p.waitFor();
}
catch(IOException | InterruptedException e)
{
e.printStackTrace();
}
if (interactive)
{
return p.exitValue() + "";
}
else
{
// return the stdout of the process p
}
}
我想要做的就是回到我通過ProcessBuilder
運行的進程的標準輸出,只有在interactive
布爾設置爲false
。但是,我無法確定如何將標準輸出重定向到ProcessBuilder
的變量。請不要使用inheritIO()
,所以當我使用像adb shell
這樣的函數時,shell會顯示在我的控制檯中,這是我想要的行爲。所以基本上,至於現在,我可以看到在控制檯的標準輸出,但我不知道如何在函數中返回它,所以我可以使用這個值作爲未來的東西的變量。