Java exec execute(boolean returnOutput, boolean returnError, boolean throwException, String[] cmd, File dir, String[] env)

Here you can find the source of execute(boolean returnOutput, boolean returnError, boolean throwException, String[] cmd, File dir, String[] env)

Description

execute

License

Open Source License

Declaration

public static String execute(boolean returnOutput, boolean returnError, boolean throwException, String[] cmd,
            File dir, String[] env) 

Method Source Code

//package com.java2s;
/**********************************************************************************************
 *
 * Asprise OCR Java API//from  w  w w .  j  ava2 s  .  c om
 * Copyright (C) 1998-2015. Asprise Inc. <asprise.com>
 *
 * This file is licensed under the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation.
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * You should have received a copy of the GNU Affero General Public License.  If not, please
 * visit <http://www.gnu.org/licenses/agpl-3.0.html>.
 *
 **********************************************************************************************/

import java.io.BufferedReader;
import java.io.File;

import java.io.InputStreamReader;

public class Main {
    public static String execute(boolean returnOutput, boolean returnError, boolean throwException, String[] cmd,
            File dir, String[] env) {
        StringBuilder output = new StringBuilder();
        StringBuilder error = new StringBuilder();
        try {
            Process proc = Runtime.getRuntime().exec(cmd, env, dir);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

            String line = null;
            while ((line = stdInput.readLine()) != null) {
                if (output.length() > 0) {
                    output.append("\n");
                }
                output.append(line);
            }

            while ((line = stdError.readLine()) != null) {
                if (error.length() > 0) {
                    error.append("\n");
                }
                error.append(line);
            }

            if (returnOutput) {
                return output.toString();
            }
            if (returnError) {
                return error.toString();
            }
            return null;
        } catch (Throwable t) {
            if (throwException) {
                throw new RuntimeException(t);
            } else {
                if (System.getProperty("DEBUG") != null) {
                    t.printStackTrace();
                }
                if (returnOutput) {
                    return output.length() > 0 ? output.toString() : null;
                }
                if (returnError) {
                    return error.length() > 0 ? error.toString() : t.getMessage();
                }
                return null;
            }
        }
    }
}

Related

  1. execShell(String cmd)
  2. execShell(String shell)
  3. execSudoCommand(final String _sudoCmd, final String _pw)
  4. execSystemCommand(String[] commands, String executionPath)
  5. execToString(final String... args)
  6. execute(final String applicationName, final String workingDirectory)
  7. execute(final String command)
  8. execute(ProcessBuilder builder, String errorMsg)
  9. execute(String cmd, String input)