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:com.github.horrorho.inflatabledonkey.util.ProcessManager.java

void error(Process process) throws IOException {
    try (BufferedReader br = new BufferedReader(
            new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) {

        String error = br.lines().collect(Collectors.joining("\n"));
        if (!error.isEmpty()) {
            logger.warn("-- error() - error: {}", error);
        }/*from   w  w w  .j  ava2  s . co  m*/
    }
}

From source file:com.github.sakserv.minicluster.yarn.util.ExecShellCliParser.java

public int runCommand() throws Exception {
    String command = getCommand();
    String stdoutFile = getStdoutPath();
    String stderrFile = getStderrPath();

    Process p = Runtime.getRuntime().exec(command.split(" "));

    String stdout = getOutput(p.getInputStream());
    String stderr = getOutput(p.getErrorStream());

    writeOutputToFile(stdout, new File(stdoutFile));
    writeOutputToFile(stderr, new File(stderrFile));

    p.waitFor();// w w  w. j  av a  2 s . c om
    return p.exitValue();
}

From source file:com.thoughtworks.go.util.ProcessWrapperTest.java

private Process getMockedProcess(OutputStream outputStream) {
    Process process = mock(Process.class);
    when(process.getErrorStream()).thenReturn(mock(InputStream.class));
    when(process.getInputStream()).thenReturn(mock(InputStream.class));
    when(process.getOutputStream()).thenReturn(outputStream);
    return process;
}

From source file:com.thoughtworks.go.server.database.Migrate.java

private int exec(String[] command) {
    try {// w ww .  ja  v a  2  s.c  o m
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("About to execute commands:");
            for (String c : command) {
                LOGGER.debug(c);
            }
        }
        Process p = Runtime.getRuntime().exec(command);
        copyInThread(p.getInputStream(), quiet ? null : sysOut);
        copyInThread(p.getErrorStream(), quiet ? null : sysOut);
        p.waitFor();
        return p.exitValue();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.yahoo.rdl.maven.ProcessRunner.java

public String run(List<String> command, ProcessBuilder processBuilder) throws IOException {
    Process process = processBuilder.start();
    try (StreamConsumer stdout = new StreamConsumer(process.getInputStream()).start()) {
        try (StreamConsumer stderr = new StreamConsumer(process.getErrorStream()).start()) {
            if (!process.waitFor(10, TimeUnit.SECONDS)) {
                throw new IOException("Process took longer than 10 seconds to execute: " + command);
            }/*from   w  w  w.j a v a  2 s .  c  om*/
            if (process.exitValue() != 0) {
                String s = stderr.getContents();
                throw new IOException("command '" + StringUtils.join(command, " ") + "' produced error: " + s);
            }
        }
        return stdout.getContents();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }

}

From source file:io.cloudslang.content.utilities.util.ProcessExecutor.java

private void stopProcess(Process process, Future<ProcessResponseEntity> futureResult) {
    futureResult.cancel(true);// w w  w . java2  s  . c  om
    process.destroy();

    closeQuietly(process.getInputStream());
    closeQuietly(process.getErrorStream());
}

From source file:com.emc.ecs.sync.filter.ShellCommandFilter.java

@Override
public void filter(SyncObject obj) {
    getNext().filter(obj);// w ww  .j  a v a2  s .c om

    String[] cmdLine = new String[] { command, obj.getSourceIdentifier(), obj.getTargetIdentifier() };
    try {
        Process p = Runtime.getRuntime().exec(cmdLine);

        InputStream stdout = p.getInputStream();
        InputStream stderr = p.getErrorStream();
        while (true) {
            try {
                int exitCode = p.exitValue();
                if (exitCode != 0) {
                    throw new RuntimeException(
                            "Command: " + Arrays.asList(cmdLine) + "exited with code " + exitCode);
                } else {
                    return;
                }
            } catch (IllegalThreadStateException e) {
                // ignore; process running
            }

            // Drain stdout and stderr.  Many processes will hang if you
            // dont do this.
            drain(stdout, System.out);
            drain(stderr, System.err);
        }
    } catch (IOException e) {
        throw new RuntimeException("Error executing command: " + Arrays.asList(cmdLine) + ": " + e.getMessage(),
                e);
    }
}

From source file:de.tarent.maven.plugins.pkg.Utils.java

/**
 * This method can be used to debug the output of processes.
 * //from   w ww .j  a v a2 s.  com
 * @param p
 */
public static void print(Process p) {
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));

    try {
        while (br.ready()) {
            System.err.println("*** Process output ***");
            System.err.println(br.readLine());
            System.err.println("**********************");
        }
    } catch (IOException ioe) {
        // foo
    }

}

From source file:ca.canucksoftware.systoolsmgr.CommandLine.java

public boolean doCmd() {
    try {/*  w  w w .j ava2 s  .co  m*/
        response = null;
        ProcessBuilder pb = new ProcessBuilder(command);
        pb.redirectErrorStream(false);
        Process p = pb.start();
        String stdout = getTextFromStream(p.getInputStream());
        String stderr = getTextFromStream(p.getErrorStream());
        if (p.waitFor() != 0) {
            returnCode = p.exitValue();
        } else {
            returnCode = 0;
        }
        if (returnCode == 0) {
            response = stdout;
        } else {
            response = stderr;
        }
    } catch (Exception e) {
        response = e.getMessage();
        returnCode = -1;
    }
    return (returnCode == 0);
}

From source file:gsu.ugahacksproject.tagg.java

public String predict(String filePath) {
    String output = null;// ww w  .ja va  2s.  c o  m
    String state = null;
    try {
        //            System.out.println(fileContent);
        // Runtime rt = Runtime.getRuntime();
        Process p = Runtime.getRuntime().exec("python " + commandPath + "/predict.py " + filePath);
        BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        String line = null;
        while ((line = error.readLine()) != null) {
            System.out.println("Python error: " + line);
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        //p.waitFor();
        //InputStream stderr = p.getInputStream();
        //InputStreamReader in = new InputStreamReader(stderr);
        //BufferedReader reader = new BufferedReader(in);
        line = null;
        while ((line = in.readLine()) != null) {
            if (output == null)
                output = line;
            else
                output += line;
        }
        int exitVal = p.waitFor();

        JSONObject jsonObj = new JSONObject(output);
        state = jsonObj.getString("state");
    } catch (Exception e) {
        e.printStackTrace();
    }

    return state;
}