Here you can find the source of runCommand(String[] command)
Parameter | Description |
---|---|
command | a parameter |
public static String runCommand(String[] command)
//package com.java2s; //License from project: Open Source License import java.io.*; public class Main { /**/*from www .ja va 2 s . c o m*/ * A new, fancy way of running external command. * Allows outputting stdout and stderr interlaced, like in real command line * Does not allow to run a command in background ;-( * @param command * @return */ public static String runCommand(String[] command) { //LOG.debug("Running: " + Arrays.toString(command)); String result = ""; try { ProcessBuilder builder = new ProcessBuilder(command); builder.redirectErrorStream(true); Process process = builder.start(); String line; BufferedReader bri = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((line = bri.readLine()) != null) { result = result + "\n" + line; //LOG.debug(line); } bri.close(); } catch (IOException ex) { ex.printStackTrace(); } return result; } /** * An old-fashioned way of running external command. * Allows to run a command in background * Does not return any feedback * @param command * @return */ public static void runCommand(final String command) { //LOG.debug("Running command " + command); Thread t = new Thread(new Runnable() { public void run() { try { Thread.sleep(10); Process process = Runtime.getRuntime().exec(command); BufferedReader bri = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader bre = new BufferedReader(new InputStreamReader(process.getErrorStream())); String lineI, lineE = null; while ((lineI = bri.readLine()) != null || (lineE = bri.readLine()) != null) { //result = result + line; if (lineI != null) { //LOG.debug(lineI); } if (lineE != null) { //LOG.debug(lineE); } } bri.close(); bre.close(); } catch (Exception e) { e.printStackTrace(); } } }); t.start(); } }