Example usage for java.lang Process getErrorStream

List of usage examples for java.lang Process getErrorStream

Introduction

In this page you can find the example usage for java.lang Process getErrorStream.

Prototype

public abstract InputStream getErrorStream();

Source Link

Document

Returns the input stream connected to the error output of the process.

Usage

From source file:myproject.Model.Common.ProcessExecutor.java

private static String properExecute(File file, String... commands) throws IOException, InterruptedException {
    ProcessBuilder builder = new ProcessBuilder(commands);
    if (file != null) {
        builder.directory(file);/*from  w  w w .j  a v a 2s  . com*/
    }
    Process process = builder.start();
    String input = IOUtils.toString(process.getInputStream(), "utf-8");
    String error = IOUtils.toString(process.getErrorStream(), "utf-8");
    //String command = Arrays.toString(builder.command().toArray(new String[] {}));
    process.waitFor();
    process.getInputStream().close();
    if (!error.isEmpty()) {
        Log.errorLog(error, ProcessExecutor.class);
    }
    String result = input;
    if (input.isEmpty()) {
        result = error;
    }
    return result;
}

From source file:design.process.ProcessUtil.java

/***/
public static String executeResult(final File workDir, final String... termArray)
        throws IOException, InterruptedException {
    final Process process = executeProcess(workDir, termArray);
    final String input = IOUtils.toString(process.getInputStream(), "UTF-8");
    final String error = IOUtils.toString(process.getErrorStream(), "UTF-8");
    final String result = input + error;
    return result;
}

From source file:hu.bme.mit.trainbenchmark.sql.process.MySqlProcess.java

public static void run(final String command[]) throws IOException, InterruptedException {
    final ProcessBuilder pb = new ProcessBuilder(command);
    final Process p = pb.start();
    p.waitFor();//from w  ww . j av  a  2 s.  co m

    final String stdOut = getInputAsString(p.getInputStream());
    final String stdErr = getInputAsString(p.getErrorStream());
    // System.out.println(stdOut);
    // System.out.println(stdErr);
}

From source file:Main.java

/**
 * create a child process with environment variables
 * @param command/*from  w w  w  .  jav  a 2  s . c o  m*/
 * @param envVars
 * @return
 * @throws InterruptedException
 * @throws IOException
 */
public static String execUnixCommand(String[] command, Map<String, String> envVars)
        throws InterruptedException, IOException {

    /*
    ProcessBuilder processBuilder = new ProcessBuilder(command);
            
    if ( envVars != null ){
    Map<String, String> env = processBuilder.environment();
    env.clear();
    env.putAll(envVars);
    }
            
    Process process = processBuilder.start();
    processBuilder.redirectErrorStream(false);
    process.waitFor();
    String outputWithError = loadStream(process.getInputStream()) + loadStream(process.getErrorStream());
    return outputWithError;
     */

    ProcessBuilder processBuilder = new ProcessBuilder(command);

    if (envVars != null) {
        Map<String, String> env = processBuilder.environment();
        env.clear();
        env.putAll(envVars);
    }
    Process process = processBuilder.start();
    String output = loadStream(process.getInputStream());
    String error = loadStream(process.getErrorStream());
    process.waitFor();
    return output + "\n" + error;
}

From source file:com.impetus.ankush.agent.utils.CommandExecutor.java

/**
 * Method to execute command arrays.//from w  ww  .j a  v a2  s . co  m
 * 
 * @param command
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
public static Result executeCommand(String... command) throws IOException, InterruptedException {
    Result rs = new Result();
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);
    StreamWrapper error = new StreamWrapper(proc.getErrorStream());
    StreamWrapper output = new StreamWrapper(proc.getInputStream());

    error.start();
    output.start();
    error.join(SLEEP_TIME);
    output.join(SLEEP_TIME);
    proc.waitFor();
    rs.setExitVal(proc.exitValue());
    rs.setOutput(output.getMessage());
    rs.setError(error.getMessage());
    proc.destroy();
    return rs;
}

From source file:com.impetus.ankush.agent.utils.CommandExecutor.java

/**
 * Method executeCommand.// w  w  w .j a v a 2s .co  m
 * 
 * @param command
 *            String
 * @return Result
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws InterruptedException
 *             the interrupted exception
 */
public static Result executeCommand(String command) throws IOException, InterruptedException {
    Result rs = new Result();
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);
    StreamWrapper error = new StreamWrapper(proc.getErrorStream());
    StreamWrapper output = new StreamWrapper(proc.getInputStream());

    error.start();
    output.start();
    error.join(SLEEP_TIME);
    output.join(SLEEP_TIME);
    proc.waitFor();
    rs.setExitVal(proc.exitValue());
    rs.setOutput(output.getMessage());
    rs.setError(error.getMessage());
    proc.destroy();
    return rs;
}

From source file:Main.java

public static int exec(String command) {
    Process proc = null;
    BufferedReader error = null;// w ww.  java 2 s . c  o  m
    try {
        proc = Runtime.getRuntime().exec(command);
        error = toReader(proc.getErrorStream());
        int retVal = proc.waitFor();
        if (retVal != 0) {
            String line;
            while ((line = error.readLine()) != null) {
                Log.d(TAG, "exec error:" + line);
            }
        }
        return retVal;
    } catch (Exception e) {
        Log.d(TAG, e.getMessage());
        if (proc != null) {
            proc.destroy();
        }
    }
    return -1;
}

From source file:com.alibaba.jstorm.utils.SystemOperation.java

public static String exec(String cmd) throws IOException {
    LOG.debug("Shell cmd: " + cmd);
    Process process = new ProcessBuilder(new String[] { "/bin/bash", "-c", cmd }).start();
    try {//  w w w .  ja  v  a2  s.co m
        process.waitFor();
        String output = IOUtils.toString(process.getInputStream());
        String errorOutput = IOUtils.toString(process.getErrorStream());
        LOG.debug("Shell Output: " + output);
        if (errorOutput.length() != 0) {
            LOG.error("Shell Error Output: " + errorOutput);
            throw new IOException(errorOutput);
        }
        return output;
    } catch (InterruptedException ie) {
        throw new IOException(ie.toString());
    }

}

From source file:de.uni.bremen.monty.moco.Main.java

private static void runExecutable(File executable) throws IOException, InterruptedException {
    ProcessBuilder processBuilder = new ProcessBuilder(executable.getAbsolutePath());

    String readFromFile = System.getProperty("testrun.readFromFile");
    if (readFromFile != null) {
        processBuilder.redirectInput(new File(readFromFile));
    } else {//from w ww  . j a v a2  s. c  o  m
        processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
    }
    Process process = processBuilder.start();
    System.err.print(IOUtils.toString(process.getErrorStream()));
    System.out.print(IOUtils.toString(process.getInputStream()));
}

From source file:de.uni.bremen.monty.moco.Main.java

private static void runCode(File llvmCode) throws IOException {
    ProcessBuilder processBuilder = new ProcessBuilder("lli", llvmCode.getAbsolutePath());
    String readFromFile = System.getProperty("testrun.readFromFile");
    if (readFromFile == null) {
        processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
    } else {/* www  .  j  a  v a2  s. c  o m*/
        processBuilder.redirectInput(new File(readFromFile));
    }
    Process process = processBuilder.start();

    System.err.print(IOUtils.toString(process.getErrorStream()));
    System.out.print(IOUtils.toString(process.getInputStream()));
}