Example usage for java.lang Process exitValue

List of usage examples for java.lang Process exitValue

Introduction

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

Prototype

public abstract int exitValue();

Source Link

Document

Returns the exit value for the process.

Usage

From source file:io.fabric8.maven.rt.BaseBoosterIT.java

protected int exec(String command) throws IOException, InterruptedException {
    Process child = Runtime.getRuntime().exec(command);
    child.waitFor();//from ww w .ja  v a 2 s .  c  om

    BufferedReader reader = new BufferedReader(new InputStreamReader(child.getInputStream()));
    String line = null;
    while ((line = reader.readLine()) != null) {
        logger.info(line);
    }

    if (child.exitValue() != 0)
        logger.log(Level.WARNING,
                String.format("Exec for : %s returned status : %d", command, child.exitValue()));
    return child.exitValue();
}

From source file:org.kududb.client.MiniKuduCluster.java

/**
 * Starts a process using the provided command and configures it to be daemon,
 * redirects the stderr to stdout, and starts a thread that will read from the process' input
 * stream and redirect that to LOG.//from  w  w w . ja va  2 s .co  m
 * @param command Process and options
 * @return The started process
 * @throws Exception Exception if an error prevents us from starting the process,
 * or if we were able to start the process but noticed that it was then killed (in which case
 * we'll log the exit value).
 */
private Process configureAndStartProcess(String[] command) throws Exception {
    LOG.info("Starting process: {}", Joiner.on(" ").join(command));
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.redirectErrorStream(true);
    Process proc = processBuilder.start();
    ProcessInputStreamLogPrinterRunnable printer = new ProcessInputStreamLogPrinterRunnable(
            proc.getInputStream());
    Thread thread = new Thread(printer);
    thread.setDaemon(true);
    thread.setName(command[0]);
    PROCESS_INPUT_PRINTERS.add(thread);
    thread.start();

    Thread.sleep(300);
    try {
        int ev = proc.exitValue();
        throw new Exception(
                "We tried starting a process (" + command[0] + ") but it exited with " + "value=" + ev);
    } catch (IllegalThreadStateException ex) {
        // This means the process is still alive, it's like reverse psychology.
    }
    return proc;
}

From source file:org.gradle.launcher.daemon.logging.DaemonGreeter.java

public void verifyGreetingReceived(Process process) {
    List<String> lines;
    try {/*from w w w  . j  av  a 2 s  .  c o m*/
        lines = IOUtils.readLines(process.getInputStream());
    } catch (Exception e) {
        throw new GradleException("Unable to get a greeting message from the daemon process."
                + " Most likely the daemon process cannot be started.", e);
    }

    String lastMessage = lines.get(lines.size() - 1);
    if (!lastMessage.equals(DaemonMessages.PROCESS_STARTED)) {
        // consider waiting a bit for the exit value
        // if exit value not provided warn that the daemon didn't exit
        int exitValue;
        try {
            exitValue = process.exitValue();
        } catch (IllegalThreadStateException e) {
            throw new GradleException(DaemonMessages.UNABLE_TO_START_DAEMON
                    + " However, it appears the process hasn't exited yet." + "\n" + processOutput(lines));
        }
        throw new GradleException(DaemonMessages.UNABLE_TO_START_DAEMON + " The exit value was: " + exitValue
                + "." + "\n" + processOutput(lines));
    }
}

From source file:perflab.LoadrunnerWrapper.java

/**
 * @param command - command to execute/*from  w  w  w.  ja  va 2s. c  o m*/
 * @return command exit code
 */
private int runCommand(String command) {
    int exitCode = -1;
    getLog().info("Command to run: " + command);

    try {
        Process p = Runtime.getRuntime().exec(command);
        p.waitFor();
        exitCode = p.exitValue();
    } catch (Exception err) {
        err.printStackTrace();
    }

    getLog().info("Exit value: " + exitCode);
    return exitCode;
}

From source file:org.batoo.jpa.community.test.BaseCoreTest.java

private void exec(String cmd) {
    try {/*w  ww.  j a  v  a 2 s  . c  o m*/
        final Process process = Runtime.getRuntime().exec(cmd);
        if (process.waitFor() != 0) {
            BaseCoreTest.LOG.error("Command failed: " + process.exitValue());
            IOUtils.copy(process.getErrorStream(), System.err);
        }

        IOUtils.copy(process.getErrorStream(), System.out);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.scilla.QueuedProcess.java

/**
 * Create and executed a queued OS process.  The process is
 * created when a semaphore is below the configured value.
 * @param args command arguments//from  ww  w. j  a va  2s . c  om
 * @param envp array of strings, each element of which has
 * environment variable settings in format name=value
 * @param dir working directory
 * @throws IOException when execution fails
 * @see java.lang.Runtime#exec(String[])
 * @see #MAX_RUNNERS_KEY
 */
public QueuedProcess(String[] args, String[] envp, File dir) throws IOException {
    // attache wrapper
    String[] wrapper = config.getStringArray(WRAPPER_KEY);
    if (wrapper != null) {
        String[] targs = new String[wrapper.length + args.length];
        System.arraycopy(wrapper, 0, targs, 0, wrapper.length);
        System.arraycopy(args, 0, targs, wrapper.length, args.length);
        args = targs;
    }

    // log execution
    if (log.isInfoEnabled()) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < args.length; i++) {
            sb.append(args[i]);
            sb.append(' ');
        }
        log.info("process: " + sb);
    }

    if (log.isDebugEnabled() && envp != null) {
        if (envp != null) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < envp.length; i++) {
                sb.append(envp[i]);
                sb.append(' ');
            }
            log.debug("env: " + sb);
        }
        if (dir != null) {
            log.debug("dir: " + dir);
        }
    }

    // execute process
    final String[] args_ = args;
    final String[] envp_ = envp;
    final File dir_ = dir;
    thrd = new Thread() {
        public void run() {
            try {
                sem.decr();
                log.debug("process started");
                Process proc = Runtime.getRuntime().exec(args_, envp_, dir_);

                // redirect stdout and stderr
                stdout = new OutputLog(proc.getInputStream());
                stdout.start();
                stderr = new OutputLog(proc.getErrorStream());
                stderr.start();

                // wait for process to finish
                proc.waitFor();
                // record exit value
                exitValue = proc.exitValue();
            } catch (Throwable ex) {
                log.error("process execution failed", ex);

                // record exception
                crash = ex;
            } finally {
                log.debug("process finished");
                sem.incr();
            }
        }
    };
    thrd.setDaemon(true);
    thrd.start();
}

From source file:org.apache.pig.test.Util.java

public static ProcessReturnInfo executeJavaCommandAndReturnInfo(String cmd) throws Exception {
    String javaHome = System.getenv("JAVA_HOME");
    if (javaHome != null) {
        String fileSeparator = System.getProperty("file.separator");
        cmd = javaHome + fileSeparator + "bin" + fileSeparator + cmd;
    }//from   w w w. j a  v  a 2  s.co m
    Process cmdProc = Runtime.getRuntime().exec(cmd);
    ProcessReturnInfo pri = new ProcessReturnInfo();
    ReadStream stdoutStream = new ReadStream(cmdProc.getInputStream());
    ReadStream stderrStream = new ReadStream(cmdProc.getErrorStream());
    stdoutStream.start();
    stderrStream.start();
    cmdProc.waitFor();
    pri.exitCode = cmdProc.exitValue();
    pri.stdoutContents = stdoutStream.getMessage();
    pri.stderrContents = stderrStream.getMessage();
    return pri;
}

From source file:org.scantegrity.scanner.ScannerController.java

/**
 * Determine if a process is alive based on the PID number.
 * //  w w  w  .  j av  a  2s  . co  m
 * @param p_pid
 * @return
 */
private boolean isAlive(int p_pid) {
    Process l_p;
    try {
        synchronized (this) {
            l_p = Runtime.getRuntime().exec(String.format(c_sigcmd, p_pid));
            l_p.waitFor();
            if (l_p.exitValue() == 0) {
                return true;
            }
        }
    } catch (Exception l_e) {
        //Nothing.
    }
    return false;
}

From source file:net.sf.nvn.plugin.BootstrapperMojo.java

@Override
void postExec(int execution, Process process) throws MojoExecutionException {
    if (execution == 0 && process.exitValue() == 0) {
        File tmpBootstrapExe = new File(
                String.format("%s\\bin\\%s\\%s.exe", this.tmpProjDir, getBuildConfig(), this.outputFileName));

        this.bootstrapExe = new File(super.mavenProject.getBuild().getDirectory(), tmpBootstrapExe.getName());

        try {/*from   w w w  . j ava2  s.  c om*/
            FileUtils.copyFile(tmpBootstrapExe, bootstrapExe);
        } catch (IOException e) {
            throw new MojoExecutionException(
                    String.format("Error copying %s to %s", tmpBootstrapExe, bootstrapExe), e);
        }

        /*
         * try { FileUtils.deleteDirectory(this.tmpProjDir); } catch
         * (IOException e) { throw new
         * MojoExecutionException("Error deleting " + this.tmpProjDir, e); }
         */

        if (this.demandUac) {
            final String format = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n"
                    + "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">\r\n"
                    + "   <assemblyIdentity version=\"%1$s\" processorArchitecture=\"X86\" name=\"%2$s\" type=\"win32\"/>\r\n"
                    + "    <description>%3$s</description>\r\n"
                    + "      <trustInfo xmlns=\"urn:schemas-microsoft-com:asm.v3\">\r\n"
                    + "         <security>\r\n" + "            <requestedPrivileges>\r\n"
                    + "               <requestedExecutionLevel level=\"requireAdministrator\"/>\r\n"
                    + "            </requestedPrivileges>\r\n" + "         </security>\r\n"
                    + "      </trustInfo>\r\n" + "</assembly>";

            String exePath = bootstrapExe.toString();
            String exeBaseName = FilenameUtils.getBaseName(exePath);
            String exeExtension = FilenameUtils.getExtension(exePath);

            String text = String.format(format, super.getNvnVersion(), exeBaseName,
                    super.mavenProject.getDescription());

            File tmpDir = new File(System.getProperty("java.io.tmpdir"));

            this.manifest = new File(tmpDir, String.format("%s.%s.manifest", exeBaseName, exeExtension));

            try {
                FileUtils.writeStringToFile(this.manifest, text);
            } catch (IOException e) {
                throw new MojoExecutionException("Error writing manifest file: " + this.manifest);
            }

            debug("wrote manifest file: " + this.manifest);
        }
    } else if (execution == 0 && process.exitValue() != 0) {
        debug("disabling demanduac because bootstrapper exit code: %s", process.exitValue());
        this.demandUac = false;
    }
}

From source file:org.batoo.jpa.core.test.BaseCoreTest.java

/**
 * Executes an artbitrary command as external process and waits for the termination.
 * //w w w . j  av  a2 s  . c  o m
 * @param cmd
 *            the command to execute
 * 
 * @since 2.0.0
 */
protected void exec(String cmd) {
    try {
        final Process process = Runtime.getRuntime().exec(cmd);
        if (process.waitFor() != 0) {
            BaseCoreTest.LOG.error("Command failed: " + process.exitValue());
            IOUtils.copy(process.getErrorStream(), System.err);
        }

        IOUtils.copy(process.getErrorStream(), System.out);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}