| 42 | } |
| 43 | |
| 44 | public static String execute(String command) throws InterruptedException, IOException { |
| 45 | File working_dir = new File("/"); |
| 46 | String[] environment = null; |
| 47 | |
| 48 | // executes the command using `sh -c` command (so that piping features are present) |
| 49 | Process proc = Runtime.getRuntime().exec(new String[] { "sh", "-c", command }, environment, working_dir); |
| 50 | |
| 51 | // read the output and error streams |
| 52 | CommandReader stdout = new CommandReader(proc.getInputStream()); |
| 53 | CommandReader stderr = new CommandReader(proc.getErrorStream()); |
| 54 | |
| 55 | // Run streams |
| 56 | stdout.start(); |
| 57 | stderr.start(); |
| 58 | |
| 59 | // ... and wait for the process to finish |
| 60 | proc.waitFor(); |
| 61 | |
| 62 | stdout.join(); |
| 63 | stderr.join(); |
| 64 | |
| 65 | // Collect results |
| 66 | String return_value = stderr.getOutput(); |
| 67 | return_value += stdout.getOutput(); |
| 68 | |
| 69 | stdout.close(); |
| 70 | stderr.close(); |
| 71 | return return_value; |
| 72 | } |
| 73 | |
| 74 | } |