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:ape.TouchCommand.java

private boolean writeSTDOut(Process p) {
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String count;/*from   w  w  w  .j  a v  a2  s.c  o  m*/
    try {
        while ((count = stdInput.readLine()) != null) {
            System.out.println(count);
        }
        stdInput.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:com.pureinfo.srm.srm2rpms.action.ExportAction.java

public ActionForward executeAction() throws PureException {
    if (BuildInfo.isUpdated()) {
        //0. to get the parameter
        String[] tables = request.getParameterValues("data");
        if (tables == null || tables.length == 0) {
            throw new PureException(PureException.INVALID_REQUEST, "");
        }//  w w w.  j  a v  a2s  .co  m

        try {
            //1. to read database info
            Properties props = DolphinConfigHelper.lookupConnectionProviderCfgByName("Export");
            String sDBName = props.getProperty(DolphinEnvironment.DATABASE);
            String sPassword = props.getProperty(DolphinEnvironment.PASSWORD);
            String sPort = props.getProperty("port");

            //2. to execute command to dump sql script
            String sPath = ClassResourceUtil.mapFullPath("mysqldump.exe", true);
            String sCommand = sPath.substring(1) + " -u root -p" + sPassword + " -P " + sPort
                    + " --compatible=mysql323 " + sDBName;
            sCommand += " " + StringUtils.join(tables, ' ');
            logger.debug("do dump sql script: " + sCommand);

            Process process = Runtime.getRuntime().exec(sCommand);
            InputStream is = process.getInputStream();

            //3. to download
            response.setContentType("application/rpms");
            response.setHeader("Content-Disposition", "attachment; filename=srm-export.rpms");
            OutputStream os = response.getOutputStream();
            IOUtil.transfer(is, os);
            is.close();
            os.close();
        } catch (Exception ex) {
            throw new PureException(0, "dump sql script from school", ex);
        }
    }
    return null;
}

From source file:ezbake.deployer.publishers.local.BaseLocalPublisher.java

protected void dumpIO(Process process) {
    inheritIO(process.getInputStream(), System.out);
    inheritIO(process.getErrorStream(), System.err);
}

From source file:jeffaschenk.tomcat.instance.generator.builders.TomcatInstanceBuilderHelper.java

/**
 * Execute a Local System Command and pull in it's response.
 *
 * @param execCommand System command to be executed.
 * @return String containing contents of system command response.
 * @throws IOException thrown if IO Exception occurs.
 *//*from w  w w . j  a va2 s .c  om*/
protected static String execReadToString(String execCommand) throws IOException {
    Process proc = Runtime.getRuntime().exec(execCommand);
    try (InputStream stream = proc.getInputStream()) {
        try (Scanner s = new Scanner(stream).useDelimiter("\\A")) {
            return s.hasNext() ? s.next() : "";
        }
    }
}

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 ww . j  av  a2  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:org.apache.asterix.test.aql.TestExecutor.java

private static String executeVagrantScript(ProcessBuilder pb, String node, String scriptName) throws Exception {
    pb.command("vagrant", "ssh", node, "--", pb.environment().get("SCRIPT_HOME") + scriptName);
    Process p = pb.start();/*  ww  w  .  jav a2s .  com*/
    p.waitFor();
    InputStream input = p.getInputStream();
    return IOUtils.toString(input, StandardCharsets.UTF_8.name());
}

From source file:org.apache.asterix.test.aql.TestExecutor.java

private static String executeVagrantManagix(ProcessBuilder pb, String command) throws Exception {
    pb.command("vagrant", "ssh", "cc", "--", pb.environment().get("MANAGIX_HOME") + command);
    Process p = pb.start();/*from   w w  w . j  a v a  2s  .  c  om*/
    p.waitFor();
    InputStream input = p.getInputStream();
    return IOUtils.toString(input, StandardCharsets.UTF_8.name());
}

From source file:com.st.symfony.Symfony.java

public void run(final String command, final long replyTimeout) throws Exception {

    String[] commands = command.split("\\s+");

    ProcessBuilder pb = new ProcessBuilder(commands);
    File log = new File(this.logFilePath);
    pb.redirectErrorStream(true);//from w  ww  .  java 2s .com
    pb.redirectOutput(Redirect.appendTo(log));
    Process p = pb.start();

    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String line = null;
    StringBuilder output = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        System.out.println(line + "\n");
        output.append(line + "\n");
    }

    p.waitFor();

}

From source file:com.sixt.service.framework.logging.ServicePropertiesProvider.java

/**
 * If running in docker, use that; else, generate test_service
 *//*from ww  w.  j  ava 2s .  co m*/
private void parseServiceInstance() {
    try {
        ProcessBuilder pb = new ProcessBuilder("bash", "-c",
                "cat /proc/self/cgroup | grep docker | sed 's/^.*\\///' | tail -n1 | cut -c 1-12");
        Process p = pb.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String output = reader.readLine();
        if (!StringUtils.isBlank(output)) {
            serviceInstanceId = output.trim();
        }
        p.waitFor();
    } catch (Exception e) {
        System.err.println("Error getting docker container id");
        e.printStackTrace();
    }
    if (serviceInstanceId == null) {
        serviceInstanceId = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 12);
    }
}

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

private int exec(String[] command) {
    try {/*from ww  w. j  av  a 2 s  .com*/
        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);
    }
}