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:br.com.autonomiccs.autonomic.plugin.common.utils.ShellCommandUtils.java

/**
 * It executes the specified shell command and wait for the
 * end of command execution to continue with the application
 * flow./*  w w  w.  jav  a2 s  .  c om*/
 *
 * If an exception happens, it will get logged and the flow of execution continues.
 * This method will not break the flow of execution if an expected exception happens.
 *
 * @param command
 *            The command that will be executed.
 * @return
 *         A <code>String</code> that is the result from
 *         command executed.
 */
public String executeCommand(String command) {
    Writer output = new StringWriter();
    try {
        Process p = Runtime.getRuntime().exec(command);
        p.waitFor();
        IOUtils.copy(p.getInputStream(), output);
    } catch (IOException | InterruptedException e) {
        logger.error(String.format("An error happened while executing command[%s]", command), e);
    }
    return output.toString();
}

From source file:org.anarres.qemu.image.QEmuImage.java

@Nonnull
public QEmuImageInfo query() throws IOException {
    ProcessBuilder builder = new ProcessBuilder("qemu-img", "info", "--output=json", file.getAbsolutePath());
    Process process = builder.start();
    byte[] data = ByteStreams.toByteArray(process.getInputStream());
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(data, QEmuImageInfo.class);
}

From source file:com.github.ffremont.microservices.springboot.node.services.PsCommand.java

public PsCommandResult exec() {
    try {/*w w w  .  j a  v  a2 s  .com*/
        Process process = this.ps.start();

        return new PsCommandResult(IOUtils.toString(process.getInputStream()));
    } catch (IOException io) {
        LOG.error("Impossible de lancer la command 'ps'", io);
    }
    return null;
}

From source file:com.hortonworks.registries.storage.tool.shell.ShellMigrationExecutor.java

@Override
public void execute(Connection connection) throws SQLException {
    String scriptLocation = this.shellScriptResource.getLocationOnDisk();
    try {/*www.j  a va2s . com*/
        List<String> args = new ArrayList<String>();
        args.add(scriptLocation);
        ProcessBuilder builder = new ProcessBuilder(args);
        builder.redirectErrorStream(true);
        Process process = builder.start();
        Scanner in = new Scanner(process.getInputStream());
        System.out.println(StringUtils.repeat("+", 200));
        while (in.hasNextLine()) {
            System.out.println(in.nextLine());
        }
        int returnCode = process.waitFor();
        System.out.println(StringUtils.repeat("+", 200));
        if (returnCode != 0) {
            throw new FlywayException("script exited with value : " + returnCode);
        }
    } catch (Exception e) {
        LOG.error(e.toString());
        // Only if SQLException or FlywaySqlScriptException is thrown flyway will mark the migration as failed in the metadata table
        throw new SQLException(String.format("Failed to run script \"%s\", %s", scriptLocation, e.getMessage()),
                e);
    }
}

From source file:jeplus.EPlusWinTools.java

/**
 * /*from  ww  w .j a  v  a2  s .c  o m*/
 * @param xesoview
 * @param esofile
 * @return the exit code
 */
public static int runXEsoView(String xesoview, String esofile) {

    try {
        // Run EP-Macro executable
        String CmdLine = xesoview + " " + esofile;
        Process EPProc = Runtime.getRuntime().exec(CmdLine);

        BufferedReader ins = new BufferedReader(new InputStreamReader(EPProc.getInputStream()));

        int res = ins.read();
        if (res != -1) {
            do {
                res = ins.read();
            } while (res != -1);
            ins.close();
        }

        EPProc.waitFor();

    } catch (IOException | InterruptedException ex) {
        logger.error("", ex);
    }

    // Return Radiance exit value
    return 0;
}

From source file:it.evilsocket.dsploit.core.System.java

public static String getSuPath() {

    if (mSuPath != null)
        return mSuPath;

    try {/*from  w w w . j a  v  a  2  s.c o  m*/
        Process process = Runtime.getRuntime().exec("which su");
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;

        while ((line = reader.readLine()) != null) {
            if (line.isEmpty() == false && line.startsWith("/")) {
                mSuPath = line;
                break;
            }
        }

        return mSuPath;
    } catch (Exception e) {
        errorLogging(TAG, e);
    }

    return "su";
}

From source file:com.jbrisbin.vpc.jobsched.exe.ExeMessageHandler.java

public ExeMessage handleMessage(final ExeMessage msg) throws Exception {
    log.debug("handling message: " + msg.toString());

    List<String> args = msg.getArgs();
    args.add(0, msg.getExe());//  w  w  w.ja v a 2  s  . c om

    try {
        ProcessBuilder pb = new ProcessBuilder(args);
        pb.environment().putAll(msg.getEnv());
        pb.directory(new File(msg.getDir()));
        pb.redirectErrorStream(true);
        Process p = pb.start();

        BufferedInputStream stdout = new BufferedInputStream(p.getInputStream());
        byte[] buff = new byte[4096];
        for (int bytesRead = 0; bytesRead > -1; bytesRead = stdout.read(buff)) {
            msg.getOut().write(buff, 0, bytesRead);
        }

        p.waitFor();
    } catch (Throwable t) {
        log.error(t.getMessage(), t);
        Object errmsg = t.getMessage();
        if (null != errmsg) {
            msg.getOut().write(((String) errmsg).getBytes());
        }
    }
    return msg;
}

From source file:com.teradata.benchto.driver.macro.shell.ShellMacroExecutionDriver.java

private void printOutput(Process process, boolean stdoutAsError) throws IOException {
    logStream(process.getInputStream(), line -> {
        line = "stdout: " + line;
        if (stdoutAsError) {
            LOGGER.error(line);// w w w .j av  a 2  s.  c om
        } else {
            LOGGER.debug(line);
        }
    });
    logStream(process.getErrorStream(), line -> LOGGER.error("stderr: " + line));
}

From source file:io.schultz.dustin.service.VideoDownloaderService.java

public void download(final URL url, final OutputStream outputStream) {

    final String filename = downloaderProperties.getDownloadDir() + File.separator + "video-"
            + new Date().getTime() + ".mp4";

    List<String> cmd = new ArrayList<>();
    cmd.add(downloaderProperties.getDownloaderAbsolutePath());
    cmd.add(url.toString());//from   ww w.j a  v a2s .c  om
    cmd.add("-f mp4");
    cmd.add("-o" + filename);

    if (log.isDebugEnabled()) {
        log.debug("Running command: {}", cmd.stream().collect(Collectors.joining(" ")));
    }

    try {
        Process p = new ProcessBuilder().command(cmd).start();
        int c;
        while ((c = p.getInputStream().read()) != -1) {
            outputStream.write(c);
            outputStream.flush();
        }
    } catch (IOException e) {
        log.error("Unable to download video at {}", url, e);
    }
}

From source file:net.landora.video.programs.ProgramsAddon.java

public boolean isAvaliable(Program program) {
    String path = getConfiguredPath(program);
    if (path == null) {
        return false;
    }//ww w . ja  v a  2 s  . com

    ArrayList<String> command = new ArrayList<String>();
    command.add(path);
    command.addAll(program.getTestArguments());
    ProcessBuilder builder = new ProcessBuilder(command);
    builder.redirectErrorStream(true);

    try {
        Process p = builder.start();
        IOUtils.copy(p.getInputStream(), new NullOutputStream());
        p.waitFor();
        return true;
    } catch (Exception e) {
        log.info("Error checking for program: " + program, e);
        return false;
    }
}