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:org.sikuli.natives.LinuxUtil.java

@Override
public int close(String appName) {
    try {//from   www.  j  ava2 s  .co m
        String cmd[] = { "killall", appName };
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        return p.exitValue();
    } catch (Exception e) {
        System.out.println("[error] closeApp:\n" + e.getMessage());
        return -1;
    }
}

From source file:org.apache.metron.rest.service.impl.StormCLIWrapper.java

protected int runCommand(String[] command) throws RestException {
    ProcessBuilder pb = getProcessBuilder(command);
    pb.inheritIO();/*from   w w  w .  ja  va 2s  .c  o  m*/
    LOG.debug("Running command: cmd={}", String.join(" ", command));

    Process process;
    try {
        process = pb.start();
        process.waitFor();

    } catch (Exception e) {
        throw new RestException(e);
    }

    int exitValue = process.exitValue();
    LOG.debug("Command completed: cmd={}, exit={}", String.join(" ", command), exitValue);

    return exitValue;
}

From source file:org.sikuli.natives.LinuxUtil.java

@Override
public int switchto(String appName, int winNum) {
    if (!isAvailable(wmctrlAvail, "switchApp")) {
        return -1;
    }/* w  w  w  .j  a  va2 s .co  m*/
    try {
        String cmd[] = { "wmctrl", "-a", appName };
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        return p.exitValue();
    } catch (Exception e) {
        System.out.println("[error] switchApp:\n" + e.getMessage());
        return -1;
    }
}

From source file:org.cryptomator.ui.ExitUtil.java

/**
 * @return true if <code>defaults read -g AppleInterfaceStyle</code> has an exit status of <code>0</code> (i.e. _not_ returning "key not found").
 *//*from  ww w  .  j  a v  a2s . c om*/
private boolean isMacMenuBarDarkMode() {
    try {
        // check for exit status only. Once there are more modes than "dark" and "default", we might need to analyze string contents..
        final Process proc = Runtime.getRuntime()
                .exec(new String[] { "defaults", "read", "-g", "AppleInterfaceStyle" });
        proc.waitFor(100, TimeUnit.MILLISECONDS);
        return proc.exitValue() == 0;
    } catch (IOException | InterruptedException | IllegalThreadStateException ex) {
        // IllegalThreadStateException thrown by proc.exitValue(), if process didn't terminate
        LOG.warn("Determining MAC OS X dark mode settings failed. Assuming default (light) mode.");
        return false;
    }
}

From source file:org.xwiki.formula.internal.NativeFormulaRenderer.java

/**
 * Execute a system command./*ww  w.  j a  va 2  s .  c o m*/
 * 
 * @param commandLine the command and its arguments
 * @param cwd the directory to use as the current working directory for the executed process
 * @return {@code true} if the command succeeded (return code 0), {@code false} otherwise
 * @throws IOException if the process failed to start
 */
private boolean executeCommand(String[] commandLine, File cwd) throws IOException {
    List<String> commandList = new Vector<String>(commandLine.length);
    Collections.addAll(commandList, commandLine);

    ProcessBuilder processBuilder = new ProcessBuilder(commandList);
    processBuilder.directory(cwd);
    Process process = processBuilder.start();
    IOUtils.copy(process.getInputStream(), new NullOutputStream());

    try {
        process.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    if (process.exitValue() != 0) {
        LOGGER.debug("Error generating image: " + IOUtils.toString(process.getErrorStream()));
    }

    return process.exitValue() == 0;
}

From source file:org.sikuli.natives.LinuxUtil.java

@Override
public int close(int pid) {
    if (!isAvailable(wmctrlAvail, "closeApp")) {
        return -1;
    }/*from   ww  w.j a v a  2 s .co m*/
    String winLine[] = findWindow("" + pid, 0, SearchType.PID);
    if (winLine == null) {
        return -1;
    }
    String cmd[] = { "wmctrl", "-ic", winLine[0] };
    try {
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        return p.exitValue();
    } catch (Exception e) {
        System.out.println("[error] closeApp:\n" + e.getMessage());
        return -1;
    }
}

From source file:org.sikuli.natives.LinuxUtil.java

@Override
public int switchto(int pid, int num) {
    if (!isAvailable(wmctrlAvail, "switchApp")) {
        return -1;
    }//from www . j  ava 2 s  .c  o m
    String winLine[] = findWindow("" + pid, num, SearchType.PID);
    if (winLine == null) {
        return -1;
    }
    String cmd[] = { "wmctrl", "-ia", winLine[0] };
    try {
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        return p.exitValue();
    } catch (Exception e) {
        System.out.println("[error] switchApp:\n" + e.getMessage());
        return -1;
    }
}

From source file:com.bekwam.resignator.commands.KeytoolCommand.java

public List<KeystoreEntry> findKeystoreEntries(String keytoolExec, String keystore, String storepass)
        throws CommandExecutionException {

    List<KeystoreEntry> entries = new ArrayList<>();

    Preconditions.checkNotNull(keytoolExec);
    Preconditions.checkNotNull(keystore);
    Preconditions.checkNotNull(storepass);

    File outputFile = null;/*from  w  ww .java  2s  . c  o m*/

    try {

        String[] cmdAndArgs = { keytoolExec, "-keystore", keystore, "-storepass", storepass, "-list" };

        File resignatorDir = new File(System.getProperty("user.home"), ".resignator");

        String outputFileName = OUTPUTFILE_PREFIX
                + StringUtils.lowerCase(RandomStringUtils.randomAlphabetic(12)) + OUTPUTFILE_SUFFIX;

        outputFile = new File(resignatorDir, outputFileName);

        ProcessBuilder pb = new ProcessBuilder(cmdAndArgs);
        pb.redirectErrorStream(false);
        pb.redirectOutput(outputFile);

        Process p = pb.start();

        boolean exitted = p.waitFor(TIMEOUT_SECS, TimeUnit.SECONDS);

        if (exitted) {

            if (p.exitValue() == 0) {

                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(outputFile)));
                entries.addAll(parseKeystoreEntries(br));
                br.close();

            } else {

                String firstLine = "";
                if (outputFile != null && outputFile.exists()) {
                    BufferedReader br = new BufferedReader(new FileReader(outputFile));
                    firstLine = br.readLine();
                    br.close();
                }

                if (logger.isErrorEnabled()) {
                    logger.error("error running exec={}; firstLine={}", keytoolExec, firstLine);
                }

                throw new CommandExecutionException(
                        "Command '" + keytoolExec + "' failed to run" + newLine + firstLine);
            }

        } else {

            if (logger.isErrorEnabled()) {
                logger.error("command '" + keytoolExec + "' timed out");
            }

            throw new CommandExecutionException("Command '" + keytoolExec + "' timed out");
        }

    } catch (Exception exc) { // includes interrupted exception

        if (logger.isErrorEnabled()) {
            logger.error("error running keytool", exc);
        }

        throw new CommandExecutionException("Error running keytool command" + newLine + exc.getMessage());

    } finally {
        if (outputFile != null) {
            outputFile.delete();
        }
    }

    return entries;
}

From source file:com.google.dart.tools.debug.core.configs.DartServerLaunchConfigurationDelegate.java

private boolean isProcessDead(Process process) {
    try {//w  w  w .  j a  v  a  2s. co  m
        process.exitValue();

        return true;
    } catch (IllegalThreadStateException ex) {
        return false;
    }
}

From source file:edu.duke.cabig.c3pr.ant.UseCaseTraceabilityReport.java

public void execute() throws BuildException {
    logger.info("Executing ant task");

    File testCaseList, classpath;
    try {//w ww  .  j a  v a  2s  . c  o m
        testCaseList = buildTestCasesListFile();
    } catch (IOException e) {
        throw new BuildException("Failed to build test cases list", e);
    }
    try {
        classpath = buildClasspathFile();
    } catch (IOException e) {
        throw new BuildException("Failed to build test cases list", e);
    }

    String[] cmd = { getAptBin(), "-nocompile", "-factory",
            UseCaseTraceabilityAnnotationProcessorFactory.class.getName(), "-d", destDir.getAbsolutePath(),
            UseCaseTraceabilityAnnotationProcessorFactory.PROJECT_NAME_OPT + projectName,
            UseCaseTraceabilityAnnotationProcessorFactory.USE_CASES_ANNOTATION_CLASS_NAME_OPT
                    + useCasesAnnotationClassName,
            '@' + classpath.getAbsolutePath(), '@' + testCaseList.getAbsolutePath() };

    try {
        logger.info("Executing apt as " + Arrays.asList(cmd));
        Process process = new ProcessBuilder(cmd).redirectErrorStream(true).start();
        IOUtils.copy(process.getInputStream(), System.out);
        process.waitFor();
        if (process.exitValue() != 0) {
            throw new BuildException("apt failed (returned non-zero exit code)");
        }
    } catch (IOException e) {
        throw new BuildException("IO problem while executing apt", e);
    } catch (InterruptedException e) {
        throw new BuildException("Interrupted while waiting for apt", e);
    }

    if (!logger.isDebugEnabled()) {
        testCaseList.delete();

    } else {
        logger.debug("Retained test case list for debugging: " + testCaseList);

    }

}