Java exec executeCommand(String[] command, boolean wait)

Here you can find the source of executeCommand(String[] command, boolean wait)

Description

execute Command

License

Open Source License

Declaration

public static String executeCommand(String[] command, boolean wait)
            throws Exception 

Method Source Code

//package com.java2s;
/***************************************************************************
 * Authors:     J.M. de la Rosa Trevin (jmdelarosa@cnb.csic.es)
 *            Airen Zaldivar Peraza  (airenzp@cnb.csic.es)
 *
 * Unidad de  Bioinformatica of Centro Nacional de Biotecnologia , CSIC
 *
 * 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.//  ww w .j a v  a 2s  .  c  om
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 *
 *  All comments concerning this program package may be sent to the
 *  e-mail address 'xmipp@cnb.csic.es'
 ***************************************************************************/

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static String executeCommand(String[] command, boolean wait)
            throws Exception {
        //System.out.println(Arrays.toString(command));
        Process p = Runtime.getRuntime().exec(command);
        if (wait) {
            p.waitFor();
            return readProcessOutput(p);
        }
        return null;
    }

    public static String executeCommand(String command, boolean wait,
            String dir) throws Exception {
        //System.out.println(command);
        Process p = Runtime.getRuntime().exec(command, null, new File(dir));
        if (wait) {
            p.waitFor();
            return readProcessOutput(p);
        }
        return null;
    }

    public static String executeCommand(String command, boolean wait)
            throws Exception {
        //System.out.println(command);
        Process p = Runtime.getRuntime().exec(command);
        if (wait) {
            p.waitFor();
            return readProcessOutput(p);
        }
        return null;
    }

    public static String readProcessOutput(Process p) throws IOException {
        StringBuffer output = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                p.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }
        reader = new BufferedReader(new InputStreamReader(
                p.getErrorStream()));

        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }
        return output.toString();

    }
}

Related

  1. executeCommand(String command)
  2. executeCommand(String command)
  3. executeCommand(String command)
  4. executeCommand(String command)
  5. executeCommand(String command, boolean waitForResponse)
  6. executeCommand1(boolean isProcessBuilder, String command)
  7. executeCommand2(boolean isProcessBuilder, String command)
  8. executeCommandAndExtractStdOut(String cmd)
  9. executeCommandForceDir(String baseCommand, String osPath, File file)