Example usage for java.lang Process destroy

List of usage examples for java.lang Process destroy

Introduction

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

Prototype

public abstract void destroy();

Source Link

Document

Kills the process.

Usage

From source file:SystemKit.java

/**
 * Returns the first line of the result of a shell command.
 * Taken from UUID.//from   w  w w.  j a  va  2 s  .  c  o m
 *
 * @param commands the commands to run
 * @return the first line of the command
 * @throws IOException
 *
 * @since 3.3.3
 */
static String getFirstLineOfCommand(String[] commands) throws IOException {
    Process p = null;
    BufferedReader reader = null;
    try {
        p = Runtime.getRuntime().exec(commands);
        reader = new BufferedReader(new InputStreamReader(p.getInputStream()), 128);
        return reader.readLine();
    } finally {
        if (p != null) {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                }
            }
            try {
                p.getErrorStream().close();
            } catch (IOException ex) {
            }
            try {
                p.getOutputStream().close();
            } catch (IOException ex) {
            }
            p.destroy();
        }
    }
}

From source file:opendap.aws.glacier.BesMetadataExtractor.java

private static String runSysCommand(String sysCmd) throws IOException {

    Process p = null;

    try {//  www  .  j  av  a2  s  .c  o m
        System.out.println("COMMAND: " + sysCmd);

        Runtime rt = Runtime.getRuntime();
        p = rt.exec(sysCmd.toString());
        InputStream in = p.getInputStream();
        OutputStream out = p.getOutputStream();
        InputStream err = p.getErrorStream();

        CharArrayWriter caw = new CharArrayWriter();

        IOUtils.copy(in, caw);
        IOUtils.copy(err, System.err);

        return caw.toString();
    } finally {
        if (p != null)
            p.destroy();
    }

}

From source file:com.tascape.qa.th.Utils.java

public static void waitForProcess(final Process process, final long timeout) throws InterruptedException {
    Thread t = new Thread() {
        @Override/*from  w  w w  .  j  ava2  s  .c om*/
        public void run() {
            try {
                Thread.sleep(timeout);
            } catch (InterruptedException ex) {
                LOG.warn(ex.getMessage());
            } finally {
                if (process != null) {
                    process.destroy();
                }
            }
        }
    };
    t.setDaemon(true);
    t.start();
    if (process != null) {
        int exitValue = process.waitFor();
        LOG.trace("process {} exits with {}", process, exitValue);
    }
}

From source file:com.sinpo.xnfc.nfc.Util.java

/**
 * ??? Root??(ROOT??)//from  w  w  w . j  a  v a2s  . c  o  m
 *
 * @return ?/??Root??
 */
public static boolean upgradeRootPermission(String pkgCodePath) {
    Process process = null;
    DataOutputStream os = null;
    try {
        String cmd = "chmod 777 " + pkgCodePath;
        process = Runtime.getRuntime().exec("su"); //?root??
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(cmd + "\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (Exception e) {
        return false;
    } finally {
        try {
            if (os != null) {
                os.close();
            }
            process.destroy();
        } catch (Exception e) {
        }
    }
    return true;
}

From source file:com.tascape.qa.th.Utils.java

public static void waitForOutputLine(final Process process, String lineExpected, final long timeout)
        throws IOException {
    Thread t = new Thread() {
        @Override/*from  w  w  w .j a v a 2 s .c  o  m*/
        public void run() {
            try {
                Thread.sleep(timeout);
            } catch (InterruptedException ex) {
                LOG.warn(ex.getMessage());
            } finally {
                if (process != null) {
                    process.destroy();
                }
            }
        }
    };
    t.setName(Thread.currentThread().getName() + "-" + t.hashCode());
    t.setDaemon(true);
    t.start();

    try (BufferedReader stdIn = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
        for (String line = stdIn.readLine(); line != null;) {
            if (line.contains(lineExpected)) {
                break;
            }
            line = stdIn.readLine();
        }
    }
    t.interrupt();
}

From source file:Main.java

public static String RunExecCmd(StringBuilder sb, Boolean su) {

    String shell;/*from   w  ww  .j  a  v  a2  s.c  o  m*/
    if (su) {
        shell = "su";
    } else {
        shell = "sh";
    }

    Process process = null;

    DataOutputStream processOutput = null;
    InputStream processInput = null;
    String outmsg = new String();

    try {

        process = Runtime.getRuntime().exec(shell);
        processOutput = new DataOutputStream(process.getOutputStream());
        processOutput.writeBytes(sb.toString() + "\n");
        processOutput.writeBytes("exit\n");
        processOutput.flush();
        processInput = process.getInputStream();
        outmsg = inputStream2String(processInput, "UTF-8");
        process.waitFor();

    } catch (Exception e) {
        //Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
        //return;
    }

    finally {
        try {
            if (processOutput != null) {
                processOutput.close();
            }
            process.destroy();
        } catch (Exception e) {
        }

    }
    return outmsg;
}

From source file:com.ibm.iotf.sample.devicemgmt.device.RasPiFirmwareHandlerSample.java

/**
 * Since JDK7 doesn't take any timeout parameter, we provide an workaround
 * that wakes up every second and checks for the completion status of the process.
 * @param process/*w  w  w .  ja v  a 2 s  .c om*/
 * @param minutes
 * @return
 * @throws InterruptedException 
 */
private static boolean waitForCompletion(Process process, int minutes) throws InterruptedException {
    long timeToWait = (60 * minutes);

    int exitValue = -1;
    for (int i = 0; i < timeToWait; i++) {
        try {
            exitValue = process.exitValue();
        } catch (IllegalThreadStateException e) {
            // Process is still running
        }
        if (exitValue == 0) {
            return true;
        }
        Thread.sleep(1000);
    }
    // Destroy the process forcibly
    try {
        process.destroy();
    } catch (Exception e) {
    }

    return false;
}

From source file:io.fabric8.maven.core.util.ProcessUtil.java

private static void addShutdownHook(final Logger log, final Process process, final File command) {
    Runtime.getRuntime().addShutdownHook(new Thread(command.getName()) {
        @Override//  ww  w .  ja  va2s. c  o m
        public void run() {
            if (process != null) {
                // Trying to determine if the process is alive
                boolean alive = false;
                try {
                    process.exitValue();
                } catch (IllegalThreadStateException e) {
                    alive = true;
                }

                if (alive) {
                    log.info("Terminating process %s", command);
                    try {
                        process.destroy();
                    } catch (Exception e) {
                        log.error("Failed to terminate process %s", command);
                    }
                    /* Only available in Java 8: So disabled for now until we switch to Java 8
                    try {
                    if (process != null && process.isAlive()) {
                        process.destroyForcibly();
                    }
                    } catch (Exception e) {
                    log.error("Failed to forcibly terminate process %s", command);
                    }
                    */
                }
            }
        }
    });
}

From source file:net.sf.jasperreports.customvisualization.export.CVElementPhantomJSImageDataProvider.java

/**
 * Kill a process, if still active, after millisDelay
 * /*  www  .j  a  v a 2s  . c o  m*/
 * @param externalProcess
 * @param millisDelay
 * @return true if the process had to be terminated, false if the process
 *         exited before the timeout
 */
public static boolean killProcess(Process externalProcess, int millisDelay) {
    try {
        Thread.sleep(millisDelay);
    } catch (InterruptedException e) {
        // e.printStackTrace();
    }

    try {
        int exitValue = externalProcess.exitValue();
        if (log.isDebugEnabled()) {
            log.debug("External Process monitoring thread - exit value: " + exitValue);
        }
        return false;
    } catch (IllegalThreadStateException e) {
        if (log.isDebugEnabled()) {
            log.debug("External Process monitoring thread - destroying process");
        }
        externalProcess.destroy();
        return true;
    }
}

From source file:com.ibm.iotf.sample.client.gateway.devicemgmt.GatewayFirmwareHandlerSample.java

/**
 * Since JDK7 doesn't take any timeout parameter, we provide an workaround
 * that wakes up every second and checks for the completion status of the process.
 * @param process/*from   ww w. j  a  v a  2 s .  com*/
 * @param minutes
 * @return
 * @throws InterruptedException 
 */
public static boolean waitForCompletion(Process process, int minutes) throws InterruptedException {
    long timeToWait = (60 * minutes);

    int exitValue = -1;
    for (int i = 0; i < timeToWait; i++) {
        try {
            exitValue = process.exitValue();
        } catch (IllegalThreadStateException e) {
            // Process is still running
        }
        if (exitValue == 0) {
            return true;
        }
        Thread.sleep(1000);
    }
    // Destroy the process forcibly
    try {
        process.destroy();
    } catch (Exception e) {
    }

    return false;
}