Java exec exec(String... _command)

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

Description

exec

License

Open Source License

Declaration

public static final String exec(String... _command) throws IOException, InterruptedException 

Method Source Code


//package com.java2s;
import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class Main {
    public static final String exec(String... _command) throws IOException, InterruptedException {
        final ProcessBuilder pb = new ProcessBuilder(_command);
        final Process p = pb.start();
        p.waitFor();/*from  w w  w . j  a  v a 2 s.c  om*/
        final StringWriter writer = new StringWriter();
        writeStream(p.getInputStream(), writer);
        return writer.toString();
    }

    private static final void writeStream(InputStream _from, Writer _printTo) throws IOException {
        final Reader in = new BufferedReader(new InputStreamReader(new BufferedInputStream(_from)));
        while (in.ready())
            _printTo.write(in.read());
    }
}

Related

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