Here you can find the source of getOutputFromCommand(boolean print, List
Parameter | Description |
---|---|
Should the output also be printed to stdout as usual | |
command | The command to execute, as a list of individual arguments (do not use spaces) |
public static List<String> getOutputFromCommand(boolean print, List<String> command)
//package com.java2s; /*/*from w w w . j av a 2 s . c o m*/ * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ import java.io.IOException; import java.lang.ProcessBuilder.Redirect; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.StringJoiner; public class Main { /** * Execute a shell command and retrieve its output. * * @param print * Should the output also be printed to stdout as usual * @param command * The command to execute, as a list of individual arguments (do * not use spaces) * @return The output of the command, as one list element per line */ public static List<String> getOutputFromCommand(boolean print, List<String> command) { try { /* "echo" the command to stdout */ StringJoiner sj = new StringJoiner(" ", "$ ", ""); command.stream().forEach(sj::add); System.out.println(sj.toString()); Path tempFile = Files.createTempFile("test-output", null); ProcessBuilder builder = new ProcessBuilder(command); builder.redirectErrorStream(true); builder.redirectOutput(Redirect.to(tempFile.toFile())); Process p = builder.start(); p.waitFor(); List<String> lines = Files.readAllLines(tempFile); Files.delete(tempFile); if (print) { /* Also print the output to the console */ lines.stream().forEach(System.out::println); } else { System.out.println("(output silenced)"); } System.out.println("(returned from command)"); return lines; } catch (IOException | InterruptedException e) { return null; } } }