How to execute Bash commands and collect output from Java?

How to execute Bash commands and collect output from Java?

Hello everyone, basically I am writing the main console application and would like to be able to run commands from it, such as sudo ***, halt, ifconfig, etc.

Any insight ?.

+7
source share
2 answers

You can use the processBuilder API for this purpose. See this example .

+13
source

unverified code:

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); } 
+3
source

All Articles