Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何执行Bash命令并从Java收集输出?_Java_Bash_Process_Command - Fatal编程技术网

如何执行Bash命令并从Java收集输出?

如何执行Bash命令并从Java收集输出?,java,bash,process,command,Java,Bash,Process,Command,如何执行Bash命令并从Java收集输出 大家好,基本上我正在编写一个基本的控制台应用程序,并希望能够从中运行命令,如sudo***、halt、ifconfig等 任何洞察?您可以为此使用API。看 未测试代码: 不要忘记调用processBuilder.redirectErrorStream(true)。或者启动另一个线程来耗尽stderr。我在“示例”链接到示例的链接中没有看到任何关于bash的内容。您有其他示例链接吗?还是一面镜子? Runtime run = Runtime.getRun

如何执行Bash命令并从Java收集输出

大家好,基本上我正在编写一个基本的控制台应用程序,并希望能够从中运行命令,如sudo***、halt、ifconfig等

任何洞察?

您可以为此使用API。看

未测试代码:


不要忘记调用processBuilder.redirectErrorStream(true)。或者启动另一个线程来耗尽stderr。我在“示例”链接到示例的链接中没有看到任何关于bash的内容。您有其他示例链接吗?还是一面镜子?
Runtime run = Runtime.getRuntime();
Process pr = run.exec(bashcommand);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));

while ( ( String line ; line = buf.readLine() ) != null ) 
{
  System.out.println(line);
}