Java Shell Command runCommand(String[] command)

Here you can find the source of runCommand(String[] command)

Description

A new, fancy way of running external command.

License

Open Source License

Parameter

Parameter Description
command a parameter

Declaration

public static String runCommand(String[] command) 

Method Source Code


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

Related

  1. runCommand(String s)
  2. runCommand(String[] args)
  3. runCommand(String[] args)
  4. runCommand(String[] cmdArray, String extraPath, String libPath)
  5. runCommand(String[] command)
  6. runCommandAndWait(String command, String workingDir, String extraPath)
  7. runCommandArray(String[] command)
  8. runCommandGetOutput(String startFolder, String[] args, int[] cmdReturnValue)
  9. runCommandPrompt(final String commandLine, final IPath path)