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.tum.cs.ias.knowrob.utils.ResourceRetriever.java

/**
 * Tries to find the specified ros package by calling 'rospack find'.
 * //from w  w  w. ja  v  a  2 s  . c  om
 * @param pkgname
 *            Package to search for
 * @return Absolute path to the package or null if not found
 */
public static String findPackage(String pkgname) {

    try {
        String line;
        String cmd = "rospack find " + pkgname;
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader errReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        while ((line = errReader.readLine()) != null) {
            System.err.println(line);
        }
        BufferedReader pathreader = new BufferedReader(new InputStreamReader(p.getInputStream(), "UTF-8"));
        if ((line = pathreader.readLine()) != null) {
            return line;
        }
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    return null;
}

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

private static File buildExecutable(String outputFileName, String inputFileName, boolean compileOnly,
        String llvmCode) throws IOException, InterruptedException {
    File outputFile = null;/*from   ww w .j  a va2s. c o m*/
    if (outputFileName != null) {
        outputFile = new File(outputFileName);
    } else if (inputFileName != null) {
        outputFile = new File(FilenameUtils.removeExtension(inputFileName));
    } else if (compileOnly) {
        outputFile = File.createTempFile("output", null, null);
        outputFile.deleteOnExit();
    } else {
        outputFile = new File("output");
    }

    ProcessBuilder llcProcessBuilder = new ProcessBuilder("llc", "-O=2");
    Process llcProcess = llcProcessBuilder.start();
    PrintStream llcInput = new PrintStream(llcProcess.getOutputStream());
    llcInput.print(llvmCode);
    llcInput.close();

    ProcessBuilder ccProcessBuilder = new ProcessBuilder("cc", "-x", "assembler", "-o",
            outputFile.getAbsolutePath(), "-");
    Process ccProcess = ccProcessBuilder.start();
    IOUtils.copy(llcProcess.getInputStream(), ccProcess.getOutputStream());
    ccProcess.getOutputStream().close();

    System.err.print(IOUtils.toString(llcProcess.getErrorStream()));
    System.err.print(IOUtils.toString(ccProcess.getErrorStream()));
    return outputFile;
}

From source file:com.bukanir.android.utils.Utils.java

public static boolean isStorageVfat(Context context) {
    try {/*  w  w  w.j a va2  s. c o m*/
        String cacheDir = context.getExternalCacheDir().toString();
        List<String> items = Arrays.asList(cacheDir.split("/"));
        String path = items.get(1) + "/" + items.get(2);

        String cmd = String.format("/system/bin/mount | grep '%s'", path);
        String[] command = { "/system/bin/sh", "-c", cmd };

        Process process = Runtime.getRuntime().exec(command, null, new File("/system/bin"));
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String line;
        String output = "";
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((line = in.readLine()) != null) {
            output += line;
        }

        List<String> outputItems = Arrays.asList(output.split(" "));
        if (outputItems.size() >= 3) {
            if (outputItems.get(2).equals("vfat")) {
                return true;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java

public static String executeLocalScript(String path, List<String> args) throws Exception {
    List<String> pargs = new ArrayList<String>();
    pargs.add("/bin/bash");
    pargs.add(path);//from  w  w  w  . j ava 2  s. com
    if (args != null) {
        pargs.addAll(args);
    }
    ProcessBuilder pb = new ProcessBuilder(pargs);
    pb.environment().putAll(EventDriver.getEnvironment());
    pb.environment().put("IP_LOCATION", EventDriver.CLIENT_NODE.getClusterIp());
    Process p = pb.start();
    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
    StringWriter writer = new StringWriter();
    IOUtils.copy(bis, writer, "UTF-8");
    return writer.toString();
}

From source file:ReadTemp.java

/** Executes the given applescript code and returns the first line of the 
 output as a string *//*from   w w  w .jav  a  2  s  .co  m*/
static String doApplescript(String script) {
    String line;
    try {
        // Start applescript 
        Process p = Runtime.getRuntime().exec("/usr/bin/osascript -s o -");

        // Send applescript via stdin
        OutputStream stdin = p.getOutputStream();
        stdin.write(script.getBytes());
        stdin.flush();
        stdin.close();

        // get first line of output
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        line = input.readLine();
        input.close();

        // If we get an exit code, print it out
        if (p.waitFor() != 0) {
            System.err.println("exit value = " + p.exitValue());
            System.err.println(line);
        }
        return line;
    } catch (Exception e) {
        System.err.println(e);
    }

    return "";
}

From source file:com.krawler.portal.util.SystemEnv.java

public static Properties getProperties() {
    Properties props = new Properties();

    try {//from  www .  ja  va 2s.  com
        Runtime runtime = Runtime.getRuntime();
        Process process = null;

        String osName = System.getProperty("os.name").toLowerCase();

        if (osName.indexOf("windows ") > -1) {
            if (osName.indexOf("windows 9") > -1) {
                process = runtime.exec("command.com /c set");
            } else {
                process = runtime.exec("cmd.exe /c set");
            }
        } else {
            process = runtime.exec("env");
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;

        while ((line = br.readLine()) != null) {
            int pos = line.indexOf(StringPool.EQUAL);

            if (pos != -1) {
                String key = line.substring(0, pos);
                String value = line.substring(pos + 1);

                props.setProperty(key, value);
            }
        }
    } catch (IOException ioe) {
        logger.warn(ioe.getMessage(), ioe);
    }

    return props;
}

From source file:Main.java

private static byte[] ExecuteCommand(String command, Boolean useroot, boolean forcenew) throws Exception {
    Process p;
    DataOutputStream stdin;//from   www . j  a  v a  2s .  com
    InputStream stdout;
    ByteArrayOutputStream baos;
    int read;
    byte[] buffer;
    /*
     * If for any reason the command does not print anything we are stuck forever.
     * Make sure that we print SOMETHING ALWAYS!
     */
    command = "RESULT=$(" + command + "); if [[ $RESULT == '' ]]; then echo '#null#';else echo $RESULT;fi\n";

    p = getProcess(useroot, forcenew);

    stdin = new DataOutputStream(p.getOutputStream());
    stdout = p.getInputStream();
    buffer = new byte[BUFF_LEN];
    baos = new ByteArrayOutputStream();

    stdin.writeBytes(command);

    while (true) {
        read = stdout.read(buffer);
        baos.write(buffer, 0, read);
        if (read < BUFF_LEN) {
            //we have read everything
            break;
        }
    }
    if (forcenew) {
        stdin.writeBytes("exit\n");
        stdin.flush();
        stdin.close();
    }

    //p.waitFor();

    return baos.toByteArray();
}

From source file:net.floodlightcontroller.queuepusher.QueuePusherSwitchMapper.java

/**
 * Runs the given command//from   w  w w . ja  v  a  2s  .  c om
 * 
 * @param cmd Command to execute
 * @return 0: (int)exit code 1: (string)stdout 2: (string)stderr
 */

private static Object[] eval(String cmd) {

    Object[] rsp = new Object[3];
    Runtime rt = Runtime.getRuntime();
    Process proc = null;

    try {
        proc = rt.exec(cmd);
        proc.waitFor();
        rsp[0] = proc.exitValue();
    } catch (InterruptedException e) {
        rsp[0] = 1;
    } catch (IOException e) {
        rsp[0] = 1;
    } finally {
        if (proc == null) {
            rsp[0] = 1;
        } else {

            try {

                BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                BufferedReader stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

                String temp;
                StringBuilder sb = new StringBuilder();
                while ((temp = stdout.readLine()) != null) {
                    sb.append(temp);
                }

                rsp[1] = sb.toString();
                sb = new StringBuilder();
                while ((temp = stderr.readLine()) != null) {
                    sb.append(temp);
                }

                rsp[2] = sb.toString();

            } catch (IOException e) {
                rsp[0] = 1;
            }

        }
    }

    return rsp;

}

From source file:com.utdallas.s3lab.smvhunter.enumerate.UIEnumerator.java

public static String execSpecial(String command) throws Exception {
    String cmd[] = { "/bin/bash", "-c", command };

    logger.info("command executed: " + command);
    Process pr = Runtime.getRuntime().exec(cmd);

    String result = IOUtils.toString(pr.getInputStream());
    pr.destroy();//from   ww  w  .  j  av a  2 s.  com
    return StringUtils.strip(result);
}

From source file:com.utdallas.s3lab.smvhunter.enumerate.UIEnumerator.java

/**
 * exec external command//from   w  w w.j ava  2  s . co m
 * @param command
 * @return
 * @throws IOException
 */
public static String execCommand(String command) throws IOException {
    logger.info("command executed " + command);
    Process pr = Runtime.getRuntime().exec(command);
    String result = IOUtils.toString(pr.getInputStream());
    pr.destroy();
    return result;
}