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:com.guye.baffle.util.OS.java

public static void exec(String[] cmd) throws BaffleException {
    Process ps = null;
    try {//  w w  w.ja 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 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:Configuration.ConfigHelper.java

public static String getUserHome() {
    String home = System.getProperty("user.home");
    if (home == null || home.length() <= 0) {
        try {//  w w  w  .  j  ava  2s. c o m
            Process exec = Runtime.getRuntime().exec(new String[] { "bash", "-c", "echo $HOME" });
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IOUtils.copy(exec.getInputStream(), baos);
            exec.getInputStream().close();
            home = baos.toString();
        } catch (Exception e) {
            return null;
        }
    }
    return home;
}

From source file:pieShareAppITs.helper.ITUtil.java

public static void waitForProcessToStartup(Process process) throws Exception {
    InputStream stream = process.getInputStream();
    InputStreamReader in = new InputStreamReader(stream);
    BufferedReader reader = new BufferedReader(in);
    String line = reader.readLine();
    while (!line.equals("!loggedIn")) {
        line = reader.readLine();/*w ww . ja  va2s.com*/
    }
}

From source file:com.ms.commons.test.classloader.util.AntxconfigUtil.java

@SuppressWarnings("unchecked")
private static void autoconf(File tmp, String autoconfigPath, String autoconfigFile, PrintStream out)
        throws Exception {
    Enumeration<URL> urls = AntxconfigUtil.class.getClassLoader().getResources(autoconfigFile);
    for (; urls.hasMoreElements();) {
        URL url = urls.nextElement();
        // copy xml
        File autoconfFile = new File(tmp, autoconfigFile);
        writeUrlToFile(url, autoconfFile);
        // copy vm
        SAXBuilder b = new SAXBuilder();
        Document document = b.build(autoconfFile);
        List<Element> elements = XPath.selectNodes(document, GEN_PATH);
        for (Element element : elements) {
            String path = url.getPath();
            String vm = element.getAttributeValue("template");
            String vmPath = StringUtils.substringBeforeLast(path, "/") + "/" + vm;
            URL vmUrl = new URL(url.getProtocol(), url.getHost(), vmPath);
            File vmFile = new File(tmp, autoconfigPath + "/" + vm);
            writeUrlToFile(vmUrl, vmFile);
        }/*ww  w.j  av  a  2 s  .c o  m*/
        // call antxconfig
        String args = "";
        if (new File(DEFAULT_ANTX_FILE).isFile()) {
            args = " -u " + DEFAULT_ANTX_FILE;
        }

        Process p = Runtime.getRuntime().exec(ANTXCONFIG_CMD + args, null, tmp);
        BufferedInputStream in = new BufferedInputStream(p.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String s;
        while ((s = br.readLine()) != null) {
            out.println(new String(s.getBytes(), ENDODING));
        }
        FileUtils.deleteDirectory(new File(tmp, autoconfigPath));
    }
}

From source file:com.cloudera.sqoop.manager.MySQLTestUtils.java

/** @return the current username. */
public static String getCurrentUser() {
    // First, check the $USER environment variable.
    String envUser = System.getenv("USER");
    if (null != envUser) {
        return envUser;
    }/*  ww  w.  j a va2  s  .co m*/

    // Try `whoami`
    String[] whoamiArgs = new String[1];
    whoamiArgs[0] = "whoami";
    Process p = null;
    BufferedReader r = null;
    try {
        p = Runtime.getRuntime().exec(whoamiArgs);
        InputStream is = p.getInputStream();
        r = new BufferedReader(new InputStreamReader(is));
        return r.readLine();
    } catch (IOException ioe) {
        LOG.error("IOException reading from `whoami`: " + ioe.toString());
        return null;
    } finally {
        // close our stream.
        if (null != r) {
            try {
                r.close();
            } catch (IOException ioe) {
                LOG.warn("IOException closing input stream from `whoami`: " + ioe.toString());
            }
        }

        // wait for whoami to exit.
        while (p != null) {
            try {
                int ret = p.waitFor();
                if (0 != ret) {
                    LOG.error("whoami exited with error status " + ret);
                    // suppress original return value from this method.
                    return null;
                }
            } catch (InterruptedException ie) {
                continue; // loop around.
            }
        }
    }
}

From source file:com.acmutv.ontoqa.tool.runtime.RuntimeManager.java

/**
 * A method that runs a command on the command line of the host and captures the result from the console
 * @param command command/*w  w w.j  a v  a 2  s. c om*/
 * @return output of the executed system command
 */
public static String runCmd(String command) {
    StringBuilder cmdOutput = new StringBuilder();
    String s;

    try {
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

        while ((s = stdInput.readLine()) != null) {
            cmdOutput.append(s).append("\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }
    return cmdOutput.toString();
}

From source file:alluxio.cli.AlluxioFrameworkIntegrationTest.java

private static boolean processExists(String processName) throws Exception {
    Process ps = Runtime.getRuntime().exec(new String[] { "ps", "ax" });
    InputStream psOutput = ps.getInputStream();

    Process processGrep = Runtime.getRuntime().exec(new String[] { "grep", processName });
    OutputStream processGrepInput = processGrep.getOutputStream();
    IOUtils.copy(psOutput, processGrepInput);
    InputStream processGrepOutput = processGrep.getInputStream();
    processGrepInput.close();/*from   www  . j a  v  a2s .c o  m*/

    // Filter out the grep process itself.
    Process filterGrep = Runtime.getRuntime().exec(new String[] { "grep", "-v", "grep" });
    OutputStream filterGrepInput = filterGrep.getOutputStream();
    IOUtils.copy(processGrepOutput, filterGrepInput);
    filterGrepInput.close();

    return IOUtils.readLines(filterGrep.getInputStream()).size() >= 1;
}

From source file:es.bsc.demiurge.core.utils.CommandExecutor.java

/**
 * Executes a system command./*from   ww w .  j a  v a  2 s . c  o  m*/
 *
 * @param command the command
 * @return the result of executing the command
 */
public static String executeCommand(String command) throws IOException, InterruptedException {
    StringBuilder result = new StringBuilder();
    Process p;
    BufferedReader reader = null;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            result.append(line);
            result.append(System.getProperty("line.separator"));
        }
        return result.toString();
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:jenkins.scm.impl.subversion.SubversionSampleRepoRule.java

private static String uuid(String url) throws Exception {
    Process proc = new ProcessBuilder("svn", "info", "--xml", url).start();
    BufferedReader r = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    Pattern p = Pattern.compile("<uuid>(.+)</uuid>");
    String line;/*  w w w.  j av a 2  s. c  om*/
    while ((line = r.readLine()) != null) {
        Matcher m = p.matcher(line);
        if (m.matches()) {
            return m.group(1);
        }
    }
    throw new IllegalStateException("no output");
}

From source file:com.atlassian.labs.bamboo.git.edu.nyu.cs.javagit.client.cli.ProcessUtilities.java

/**
 * Reads the output from the process and prints it to stdout.
 *
 * @param p//from w w  w. ja  v  a2 s. co  m
 *          The process from which to read the output.
 * @exception IOException
 *              An <code>IOException</code> is thrown if there is trouble reading input from the
 *              sub-process.
 */
public static void getProcessOutput(Process p, IParser parser) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while (true) {
        try {
            String str = br.readLine();
            if (null == str) {
                break;
            }
            parser.parseLine(str);
            LOG.debug("  " + str);
        } catch (IOException e) {
            /*
             * TODO: add logging of any information already read from the InputStream. -- jhl388
             * 06.14.2008
             */
            IOException toThrow = new IOException(ExceptionMessageMap.getMessage("020101"));
            toThrow.initCause(e);
            throw toThrow;
        }
    }
}