Example usage for java.lang Process waitFor

List of usage examples for java.lang Process waitFor

Introduction

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

Prototype

public abstract int waitFor() throws InterruptedException;

Source Link

Document

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.

Usage

From source file:com.kylinolap.common.util.OSCommandExecutor.java

private void runNativeCommand() throws IOException {
    String[] cmd = new String[3];
    String osName = System.getProperty("os.name");
    if (osName.startsWith("Windows")) {
        cmd[0] = "cmd.exe";
        cmd[1] = "/C";
    } else {//from w w  w  . j  a v  a 2  s  .c  om
        cmd[0] = "/bin/bash";
        cmd[1] = "-c";
    }
    cmd[2] = command;

    ProcessBuilder builder = new ProcessBuilder(cmd);
    builder.redirectErrorStream(true);
    Process proc = builder.start();

    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    IOUtils.copy(proc.getInputStream(), buf);
    output = buf.toString("UTF-8");

    try {
        exitCode = proc.waitFor();
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
}

From source file:com.microsoft.tfs.client.common.ui.protocolhandler.ProtocolHandlerWindowsRegistrationCommand.java

private void updateRegistryIfNeeded(final String launcher) throws IOException, InterruptedException {
    if (isRegistryUpdateNeeded()) {

        log.info(MessageFormat.format("Registering {0} as the \"{1}\" protocol handler", //$NON-NLS-1$
                PROTOCOL_HANDLER_REG_VALUE, ProtocolHandler.PROTOCOL_HANDLER_SCHEME));

        final File script = createRegeditFile(launcher);

        final ProcessBuilder pb = new ProcessBuilder("cmd.exe", //$NON-NLS-1$
                "/C", //$NON-NLS-1$
                "regedit.exe /s \"" + script.getPath() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
        final Process cmd = pb.start();
        int rc = cmd.waitFor();
        log.info("   rc = " + rc); //$NON-NLS-1$

        script.delete();/*from  ww w.ja v a  2  s .  c o m*/
    }
}

From source file:io.hops.hopsworks.api.zeppelin.util.ZeppelinResource.java

private void forceKillProccess(String pid) {
    String[] command = { "kill", "-9", pid };
    ProcessBuilder pb = new ProcessBuilder(command);
    if (pid == null) {
        return;/*from   www.j  a  va2s  .  c  om*/
    }
    try {
        Process p = pb.start();
        p.waitFor();
        p.exitValue();
    } catch (IOException | InterruptedException ex) {
        logger.log(Level.WARNING, "Problem killing Zeppelin Interpreter: {0}", ex.toString());
    }
}

From source file:algorithm.OpenStegoRandomLSBSteganography.java

private void encapsulate(String cover, String message, String output) throws IOException {
    try {//from ww  w  . j a va 2  s .  c om
        String[] args = new String[] { "java", "-jar", LIBRARY_DIRECTORY + "openstego.jar", "embed", "-a",
                "RandomLSB", "-cf", cover, "-mf", message, "-sf", output, "-C", "-E" };
        Process process = Runtime.getRuntime().exec(args);
        process.waitFor();
        InputStream inputStream = process.getInputStream();
        byte b[] = new byte[inputStream.available()];
        inputStream.read(b, 0, b.length);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:algorithm.OpenStegoRandomLSBSteganography.java

private void encapsulateAndCompress(String cover, String message, String output) throws IOException {
    try {/*from www  .j a v a 2s .  c  om*/
        String[] args = new String[] { "java", "-jar", LIBRARY_DIRECTORY + "openstego.jar", "embed", "-a",
                "RandomLSB", "-cf", cover, "-mf", message, "-sf", output, "-c", "-E" };
        Process process = Runtime.getRuntime().exec(args);
        process.waitFor();
        InputStream inputStream = process.getInputStream();
        byte b[] = new byte[inputStream.available()];
        inputStream.read(b, 0, b.length);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:jeplus.INSELWinTools.java

/**
 * Call INSEL executable file to run the simulation
 * @param config INSEL Configuration// www  . j  a  v  a 2s .  c  om
 * @param WorkDir The working directory where the input files are stored and the output files to be generated
 * @param useReadVars Whether or not to use readvars after simulation
 * @return the result code represents the state of execution steps. >=0 means successful
 */
public static int runINSEL(INSELConfig config, String WorkDir, String modelfile) {

    int ExitValue = -99;

    try {
        // Trace simulation time
        Date start = new Date();

        // Run EnergyPlus executable
        String CmdLine = config.getResolvedInselEXEC() + " " + modelfile;
        Process EPProc = Runtime.getRuntime().exec(CmdLine, null, new File(WorkDir));

        BufferedReader ins = new BufferedReader(new InputStreamReader(EPProc.getInputStream()));
        // Use console output as the report file
        BufferedWriter outs = new BufferedWriter(new FileWriter(WorkDir + config.ScreenFile, false));
        outs.newLine();
        outs.write("Calling insel.exe - " + (new SimpleDateFormat()).format(start));
        outs.newLine();
        outs.write("Command line: " + WorkDir + ">" + CmdLine);
        outs.newLine();

        int res = ins.read();
        while (res != -1) {
            outs.write(res);
            res = ins.read();
        }
        ins.close();
        outs.newLine();
        outs.write("Simulation time: " + Long.toString((new Date().getTime() - start.getTime()) / 1000)
                + " seconds");
        outs.flush();
        outs.close();

        EPProc.waitFor();
        ExitValue = EPProc.exitValue();
    } catch (IOException | InterruptedException e) {
        logger.error("Exception during INSEL execution.", e);
    }

    // Return Radiance exit value
    return ExitValue;
}

From source file:io.syndesis.verifier.LocalProcessVerifier.java

private String getConnectorClasspath(Connector connector) throws IOException, InterruptedException {
    byte[] pom = new byte[0]; // TODO: Fix generation to use an Action projectGenerator.generatePom(connector);
    java.nio.file.Path tmpDir = Files.createTempDirectory("syndesis-connector");
    try {/* w  w w.j a  v a 2s. c  om*/
        Files.write(tmpDir.resolve("pom.xml"), pom);
        ArrayList<String> args = new ArrayList<>();
        args.add("mvn");
        args.add("org.apache.maven.plugins:maven-dependency-plugin:3.0.0:build-classpath");
        if (localMavenRepoLocation != null) {
            args.add("-Dmaven.repo.local=" + localMavenRepoLocation);
        }
        ProcessBuilder builder = new ProcessBuilder().command(args)
                .redirectError(ProcessBuilder.Redirect.INHERIT).directory(tmpDir.toFile());
        Map<String, String> environment = builder.environment();
        environment.put("MAVEN_OPTS", "-Xmx64M");
        Process mvn = builder.start();
        try {
            String result = parseClasspath(mvn.getInputStream());
            if (mvn.waitFor() != 0) {
                throw new IOException(
                        "Could not get the connector classpath, mvn exit value: " + mvn.exitValue());
            }
            return result;
        } finally {
            mvn.getInputStream().close();
            mvn.getOutputStream().close();
        }
    } finally {
        FileSystemUtils.deleteRecursively(tmpDir.toFile());
    }
}

From source file:hoot.services.command.CommandRunner.java

private static int handleProcessStatic(Process pProcess, String pOrigCmd, Writer pOut, Writer pErr,
        List<CharPump> stdOutErrList, MutableBoolean interrupt) throws IOException, InterruptedException {

    CharPump outpump = new CharPump(new BufferedReader(new InputStreamReader(pProcess.getInputStream())), pOut,
            pOrigCmd, interrupt);/*from  w w  w. j  av  a  2s  .  c  om*/
    stdOutErrList.add(outpump);
    CharPump errpump = new CharPump(new BufferedReader(new InputStreamReader(pProcess.getErrorStream())), pErr,
            pOrigCmd, interrupt);
    stdOutErrList.add(errpump);
    outpump.start();
    errpump.start();
    outpump.join();
    errpump.join();

    if (_log.isInfoEnabled())
        _log.info("Waiting for '" + pOrigCmd + "' to complete.");
    int status = pProcess.waitFor();

    return status;
}

From source file:io.jmnarloch.cd.go.plugin.sbt.SbtTaskExecutor.java

private int execute(ProcessBuilder builder, JobConsoleLogger console) throws IOException, InterruptedException {

    Process process = null;
    try {/*from  w  w  w .ja  v a 2  s. c  o  m*/
        process = builder.start();

        console.readOutputOf(process.getInputStream());
        console.readErrorOf(process.getErrorStream());
        return process.waitFor();
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
}

From source file:de.cosmocode.palava.store.FileSystemStore.java

private void waitAndCheck(Process process) throws InterruptedException, IOException {
    if (process.waitFor() == 0) {
        return;/* www .  j av a 2 s.c  o m*/
    } else {
        final byte[] bytes = ByteStreams.toByteArray(process.getErrorStream());
        final String message = new String(bytes, Charsets.UTF_8);
        throw new IOException(message);
    }
}