Example usage for java.lang Process getInputStream

List of usage examples for java.lang Process getInputStream

Introduction

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

Prototype

public abstract InputStream getInputStream();

Source Link

Document

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

Usage

From source file:edu.cwru.jpdg.Dotty.java

/**
 * Compiles the graph to dotty.//from   w  w  w.j  a  v a2  s  . c  o  m
 */
public static String dotty(String graph) {

    byte[] bytes = graph.getBytes();

    try {
        ProcessBuilder pb = new ProcessBuilder("dotty");
        pb.redirectError(ProcessBuilder.Redirect.INHERIT);
        Process p = pb.start();
        OutputStream stdin = p.getOutputStream();
        stdin.write(bytes);
        stdin.close();

        String line;
        BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
        List<String> stdout_lines = new ArrayList<String>();
        line = stdout.readLine();
        while (line != null) {
            stdout_lines.add(line);
            line = stdout.readLine();
        }
        if (p.waitFor() != 0) {
            throw new RuntimeException("javac failed");
        }

        return StringUtils.join(stdout_lines, "\n");
    } catch (InterruptedException e) {
        throw new RuntimeException(e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:edu.ku.brc.af.ui.ProcessListUtil.java

/**
 * @return/*from   w w  w. j av a2  s . c o m*/
 */
public static List<List<String>> getRunningProcessesUnix() {
    List<List<String>> processes = new ArrayList<List<String>>();
    try {
        Process process = Runtime.getRuntime().exec("ps aux");
        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        Integer cmdInx = null;
        while ((line = input.readLine()) != null) {
            if (cmdInx == null) {
                cmdInx = line.indexOf("COMMAND");
            }
            if (!line.trim().equals("")) {
                ArrayList<String> fields = new ArrayList<String>();
                String[] tokens = StringUtils.split(line.substring(0, cmdInx).trim(), ' ');
                for (String tok : tokens) {
                    fields.add(tok);
                }
                fields.add(line.substring(cmdInx, line.length()));
                processes.add(fields);
                //System.out.println("["+line.substring(0, cmdInx).trim()+"]["+line.substring(cmdInx, line.length())+"]");
            }
        }
        input.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return processes;
}

From source file:at.ac.tuwien.dsg.cloud.salsa.engine.utils.SystemFunctions.java

public static int executeCommandGetReturnCode(String cmd, String workingDir, String executeFrom) {
    if (workingDir == null) {
        workingDir = "/tmp";
    }/*w w  w .java  2  s  .c  o  m*/
    logger.debug("Execute command: " + cmd + ". Working dir: " + workingDir);
    try {
        String[] splitStr = cmd.split("\\s+");
        ProcessBuilder pb = new ProcessBuilder(splitStr);
        pb.directory(new File(workingDir));
        pb = pb.redirectErrorStream(true); // this is important to redirect the error stream to output stream, prevent blocking with long output
        Map<String, String> env = pb.environment();
        String path = env.get("PATH");
        path = path + File.pathSeparator + "/usr/bin:/usr/sbin";
        logger.debug("PATH to execute command: " + pb.environment().get("PATH"));
        env.put("PATH", path);

        Process p = pb.start();

        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            logger.debug(line);
        }
        p.waitFor();
        int returnCode = p.exitValue();
        logger.debug("Execute command done: " + cmd + ". Get return code: " + returnCode);
        return returnCode;
    } catch (InterruptedException | IOException e1) {
        logger.error("Error when execute command. Error: " + e1);
    }
    return -1;
}

From source file:com.baifendian.swordfish.execserver.utils.OsUtil.java

/**
 *  linux/windows /*from www  .j a  v a2s.c  o  m*/
 *
 * @param commandStr
 * @return
 * @throws IOException
 */
public static String exeCmd(String commandStr) throws IOException {
    BufferedReader br = null;

    try {
        Process p = Runtime.getRuntime().exec(commandStr);
        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        StringBuilder sb = new StringBuilder();

        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }

        return sb.toString();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (Exception e) {
                logger.error("close reader", e);
            }
        }
    }
}

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   w  w w  .j a va  2s.  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:krasa.visualvm.VisualVMHelper.java

private static SpecVersion getJavaVersion(String jdkHome) {
    try {/* ww w . ja va 2  s  .  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:de.uni_freiburg.informatik.ultimate.licence_manager.authors.SvnAuthorProvider.java

private static boolean isSvnAvailable() {
    final ProcessBuilder pbuilder = new ProcessBuilder("svn", "--version");
    try {/*from   w  w  w  .  j  a  va 2 s  .  com*/
        final Process process = pbuilder.start();
        if (process.waitFor(1000, TimeUnit.MILLISECONDS)) {
            final List<String> lines = IOUtils.readLines(process.getInputStream(), Charset.defaultCharset());
            if (lines == null || lines.isEmpty()) {
                return false;
            }
            sSvnVersion = lines.get(0);
            return true;
        }
    } catch (IOException | InterruptedException e) {
        System.err.println("Could not find 'svn' executable, disabling author provider");
        return false;
    }
    return false;
}

From source file:com.ebiznext.flume.source.TestMultiLineExecSource.java

private static List<String> exec(String command) throws Exception {
    String[] commandArgs = command.split("\\s+");
    Process process = new ProcessBuilder(commandArgs).start();
    BufferedReader reader = null;
    try {/*from  w  ww. j a  v a 2  s .c  o m*/
        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        List<String> result = Lists.newArrayList();
        String line;
        while ((line = reader.readLine()) != null) {
            result.add(line);
        }
        return result;
    } finally {
        process.destroy();
        if (reader != null) {
            reader.close();
        }
        int exit = process.waitFor();
        if (exit != 0) {
            throw new IllegalStateException("Command [" + command + "] exited with " + exit);
        }
    }
}

From source file:Main.java

public static String do_exec_with_root(String cmd) {
    String s = "\n";
    try {/*  w  w  w.  ja  v a2 s  .c o  m*/
        Process su_p = Runtime.getRuntime().exec("su");
        DataOutputStream dataOutputStream = new DataOutputStream(su_p.getOutputStream());
        dataOutputStream.writeBytes(cmd + "\n");
        dataOutputStream.writeBytes("exit" + "\n");
        dataOutputStream.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(su_p.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            s += line + "\n";
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return s;
}

From source file:com.kylinolap.metadata.tool.HiveSourceTableMgmt.java

public static InputStream executeHiveCommand(String command) throws IOException {

    ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "hive -e \"" + command + "\"");

    // Run hive/*from  w  w w  . ja v a 2s .c  o  m*/
    pb.redirectErrorStream(true);
    Process p = pb.start();
    InputStream is = p.getInputStream();

    return is;
}