Java exec exec(String... cmdarray)

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

Description

exec

License

Open Source License

Declaration

public static String exec(String... cmdarray) 

Method Source Code

//package com.java2s;
/*//  w  w  w . j a va  2 s .  c  o m
 * Utilities.java
 *
 * Copyright (C) 1998-2005 Peter Graves
 *
 * 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.
 *
 * 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.
 */

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class Main {
    public static String exec(String... cmdarray) {
        try {
            Process process = Runtime.getRuntime().exec(cmdarray);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String s;
            while ((s = reader.readLine()) != null) {
                if (s.length() > 0) {
                    if (sb.length() > 0)
                        sb.append('\n');
                    sb.append(s);
                }
            }
            process.getInputStream().close();
            process.getOutputStream().close();
            process.getErrorStream().close();
            process.waitFor();
            return sb.toString();
        } catch (Throwable t) {
            return null;
        }
    }
}

Related

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