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.cloudera.knittingboar.utils.DataUtils.java

public static synchronized File getTwentyNewsGroupDir() throws IOException {
    if (twentyNewsGroups != null) {
        return twentyNewsGroups;
    }//from ww w  .j  ava  2  s.  com
    // mac gives unique tmp each run and we want to store this persist
    // this data across restarts
    File tmpDir = new File("/tmp");
    if (!tmpDir.isDirectory()) {
        tmpDir = new File(System.getProperty("java.io.tmpdir"));
    }
    File baseDir = new File(tmpDir, TWENTY_NEWS_GROUP_LOCAL_DIR);
    if (!(baseDir.isDirectory() || baseDir.mkdir())) {
        throw new IOException("Could not mkdir " + baseDir);
    }
    File tarFile = new File(baseDir, TWENTY_NEWS_GROUP_TAR_FILE_NAME);

    if (!tarFile.isFile()) {
        FileUtils.copyURLToFile(new URL(TWENTY_NEWS_GROUP_TAR_URL), tarFile);
    }

    Process p = Runtime.getRuntime()
            .exec(String.format("tar -C %s -xvf %s", baseDir.getAbsolutePath(), tarFile.getAbsolutePath()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    System.out.println("Here is the standard error of the command (if any):\n");
    String s;
    while ((s = stdError.readLine()) != null) {
        System.out.println(s);
    }
    stdError.close();
    twentyNewsGroups = baseDir;
    return twentyNewsGroups;
}

From source file:krasa.visualvm.VisualVMHelper.java

private static SpecVersion getJavaVersion(String jdkHome) {
    try {//www  .ja  v  a2s .c o m
        String javaCmd = jdkHome + File.separator + "bin" + File.separator + "java";
        Process prc = Runtime.getRuntime().exec(new String[] { javaCmd, "-version" });

        String version = getJavaVersion(prc.getErrorStream());
        if (version == null) {
            version = getJavaVersion(prc.getInputStream());
        }
        return new SpecVersion(version);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:hudson.os.WindowsUtil.java

/**
 * Creates an NTFS junction point if supported. Similar to symbolic links, NTFS provides junction points which
 * provide different features than symbolic links.
 * @param junction NTFS junction point to create
 * @param target target directory to junction
 * @return the newly created junction point
 * @throws IOException if the call to mklink exits with a non-zero status code
 * @throws InterruptedException if the call to mklink is interrupted before completing
 * @throws AssertionError if this method is called on a non-Windows platform
 *///from   ww w . j a  va2 s  . c o  m
public static @Nonnull File createJunction(@Nonnull File junction, @Nonnull File target)
        throws IOException, InterruptedException {
    assertTrue(Functions.isWindows());
    Process mklink = execCmd("mklink", "/J", junction.getAbsolutePath(), target.getAbsolutePath());
    int result = mklink.waitFor();
    if (result != 0) {
        String stderr = IOUtils.toString(mklink.getErrorStream());
        String stdout = IOUtils.toString(mklink.getInputStream());
        throw new IOException("Process exited with " + result + "\nStandard Output:\n" + stdout
                + "\nError Output:\n" + stderr);
    }
    return junction;
}

From source file:com.thoughtworks.go.utils.CommandUtils.java

private static StringBuilder captureOutput(Process process) throws IOException, InterruptedException {
    BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
    BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    StringBuilder result = new StringBuilder();
    result.append("output:\n");
    dump(output, result);/*from www .  j  a v a  2  s .  com*/
    result.append("error:\n");
    dump(error, result);
    process.waitFor();
    return result;
}

From source file:brut.util.OS.java

public static void exec(String[] cmd) throws BrutException {
    Process ps = null;
    try {/*from   www . j a v a  2s . c om*/
        ps = Runtime.getRuntime().exec(cmd);

        new StreamForwarder(ps.getInputStream(), System.err).start();
        new StreamForwarder(ps.getErrorStream(), System.err).start();
        if (ps.waitFor() != 0) {
            throw new BrutException("could not exec command: " + Arrays.toString(cmd));
        }
    } catch (IOException ex) {
        throw new BrutException("could not exec command: " + Arrays.toString(cmd), ex);
    } catch (InterruptedException ex) {
        throw new BrutException("could not exec command: " + Arrays.toString(cmd), ex);
    }
}

From source file:com.guye.baffle.util.OS.java

public static void exec(String[] cmd) throws BaffleException {
    Process ps = null;
    try {//from  w w w.  j a v  a2s  . c om
        ps = Runtime.getRuntime().exec(cmd);

        new StreamForwarder(ps.getInputStream(), System.err).start();
        new StreamForwarder(ps.getErrorStream(), System.err).start();
        if (ps.waitFor() != 0) {
            throw new BaffleException("could not exec command: " + Arrays.toString(cmd));
        }
    } catch (IOException ex) {
        throw new BaffleException("could not exec command: " + Arrays.toString(cmd), ex);
    } catch (InterruptedException ex) {
        throw new BaffleException("could not exec command: " + Arrays.toString(cmd), ex);
    }
}

From source file:com.act.utils.ProcessRunner.java

/**
 * Run's a child process using the specified command and arguments, timing out after a specified number of seconds
 * if the process does not terminate on its own in that time.
 * @param command The process to run./*w ww.j  a  v  a  2 s.  c o m*/
 * @param args The arguments to pass to that process.
 * @param timeoutInSeconds A timeout to impose on the child process; an InterruptedException is likely to occur
 *                         when the child process times out.
 * @return The exit code of the child process.
 * @throws InterruptedException
 * @throws IOException
 */
public static int runProcess(String command, List<String> args, Long timeoutInSeconds)
        throws InterruptedException, IOException {
    /* The ProcessBuilder doesn't differentiate the command from its arguments, but we do in our API to ensure the user
     * doesn't just pass us a single string command, which invokes the shell and can cause all sorts of bugs and
     * security issues. */
    List<String> commandAndArgs = new ArrayList<String>(args.size() + 1) {
        {
            add(command);
            addAll(args);
        }
    };
    ProcessBuilder processBuilder = new ProcessBuilder(commandAndArgs);
    LOGGER.info("Running child process: %s", StringUtils.join(commandAndArgs, " "));

    Process p = processBuilder.start();
    // Log whatever the child writes.
    new StreamLogger(p.getInputStream(), l -> LOGGER.info("[child STDOUT]: %s", l)).run();
    new StreamLogger(p.getErrorStream(), l -> LOGGER.warn("[child STDERR]: %s", l)).run();
    // Wait for the child process to exit, timing out if it takes to long to finish.
    if (timeoutInSeconds != null) {
        p.waitFor(timeoutInSeconds, TimeUnit.SECONDS);
    } else {
        p.waitFor();
    }

    // 0 is the default success exit code in *nix land.
    if (p.exitValue() != 0) {
        LOGGER.error("Child process exited with non-zero status code: %d", p.exitValue());
    }

    return p.exitValue();
}

From source file:edu.cmu.cs.lti.ark.fn.data.prep.ParsePreparation.java

public static void runExternalCommand(String command) {
    String s;//from   www . ja  va  2s  . co  m
    try {
        Process p = Runtime.getRuntime().exec(command);
        PrintStream errStream = System.err;
        System.setErr(System.out);
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        // read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
        p.destroy();
        System.setErr(errStream);
    } catch (IOException e) {
        System.out.println("exception happened - here's what I know: ");
        e.printStackTrace();
        System.exit(-1);
    }
}

From source file:kr.ac.kaist.wala.hybridroid.analysis.resource.AndroidDecompiler.java

private static void permission(String path) {
    String[] cmd = { "chmod", "755", path };
    ProcessBuilder pb = new ProcessBuilder();
    pb.command(cmd);//www.java  2s  .com

    Process p = null;
    try {
        p = pb.start();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        String r = null;

        while ((r = br.readLine()) != null) {
            System.out.println(r);
        }

        while ((r = bre.readLine()) != null) {
            System.err.println(r);
        }

        int res = p.waitFor();
        if (res != 0) {
            throw new InternalError("failed to decompile: " + path);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:io.jmnarloch.cd.go.plugin.gradle.GradleTaskExecutor.java

/**
 * Executes the actual Gradle build.//from   w w  w.  j  a  v a 2 s  . co m
 *
 * @param builder the process builder
 * @param console the log output
 * @return the process return value
 * @throws IOException          if any error occurs during I/O operation
 * @throws InterruptedException if any error occurs during process execution
 */
private static int execute(ProcessBuilder builder, JobConsoleLogger console)
        throws IOException, InterruptedException {

    Process process = null;
    try {
        process = builder.start();

        console.readOutputOf(process.getInputStream());
        console.readErrorOf(process.getErrorStream());
        return process.waitFor();
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
}