Java UTF8 getOutputFromCommand(boolean print, List command)

Here you can find the source of getOutputFromCommand(boolean print, List command)

Description

Execute a shell command and retrieve its output.

License

Open Source License

Parameter

Parameter Description
print 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)

Return

The output of the command, as one list element per line

Declaration

public static List<String> getOutputFromCommand(boolean print, List<String> command) 

Method Source Code


//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;
        }
    }
}

Related

  1. fromUTF8(byte[] bytes)
  2. generateRawUTF8Bytes(final String string)
  3. getBytesUtf8(final String string)
  4. getFile(String outputFolder, String fileName)
  5. getFiles(String InputFilePath)
  6. getStringFromUTF8Bytes(byte[] utf8Bytes)
  7. getUtf8()
  8. getUTF8()
  9. getUtf8()