Java exec exec(String... args)

Here you can find the source of exec(String... args)

Description

A simple debugging utility used to obtain information about the execution environment that's only available through some system utility, like netstat, or jps, etc.

License

Open Source License

Return

a string denoting the output from the command. Or a string prefixed by the word EXCEPTION, if an exception was encountered, followed by the exception class name and exception message.

Declaration

public static String exec(String... args) 

Method Source Code

//package com.java2s;
/*-//  w  w w  .  j a  va 2s  .  c o m
 *
 *  This file is part of Oracle Berkeley DB Java Edition
 *  Copyright (C) 2002, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 *  Oracle Berkeley DB Java Edition is free software: you can redistribute it
 *  and/or modify it under the terms of the GNU Affero General Public License
 *  as published by the Free Software Foundation, version 3.
 *
 *  Oracle Berkeley DB Java Edition 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 Affero
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License in
 *  the LICENSE file along with Oracle Berkeley DB Java Edition.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 *  An active Oracle commercial licensing agreement for this product
 *  supercedes this license.
 *
 *  For more information please contact:
 *
 *  Vice President Legal, Development
 *  Oracle America, Inc.
 *  5OP-10
 *  500 Oracle Parkway
 *  Redwood Shores, CA 94065
 *
 *  or
 *
 *  berkeleydb-info_us@oracle.com
 *
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  EOF
 *
 */

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class Main {
    /**
     * A simple debugging utility used to obtain information about the
     * execution environment that's only available through some system utility,
     * like netstat, or jps, etc. It's up to the caller to ensure the
     * availability of the utility and ensure that it's on the search path.
     *
     * @args the arguments to a ProcessBuilder with args[0] being the command
     * and args[1-...] being its args
     *
     * @return a string denoting the output from the command. Or a string
     * prefixed by the word EXCEPTION, if an exception was encountered,
     * followed by the exception class name and exception message.
     */
    public static String exec(String... args) {

        final ByteArrayOutputStream bao = new ByteArrayOutputStream(1024);
        final PrintStream output = new PrintStream(bao);

        try {

            final ProcessBuilder builder = new ProcessBuilder(args);
            builder.redirectErrorStream(true);

            final Process process = builder.start();
            final InputStream is = process.getInputStream();
            final InputStreamReader isr = new InputStreamReader(is);
            final BufferedReader br = new BufferedReader(isr);

            String line;
            while ((line = br.readLine()) != null) {
                output.println(line);
            }

        } catch (Exception e) {
            output.println("EXCEPTION:" + " class:"
                    + e.getClass().getName() + " message:" + e.getMessage());
        }

        return bao.toString();
    }
}

Related

  1. exec(String command[], boolean stop)
  2. exec(String exec)
  3. exec(String program, String... args)
  4. exec(String pwd, boolean quiet, String... command)
  5. exec(String... _command)
  6. exec(String... cmd)
  7. exec(String... cmdarray)
  8. exec(String... command)
  9. exec(String... command)