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:Main.java

public static String dumpFile_spica(String filename) {
    String line_spica = "";
    try {/* ww  w . ja  v a2  s . c om*/
        Process ifc_spica = Runtime.getRuntime().exec("cat " + filename);
        BufferedReader bis_spica = new BufferedReader(new InputStreamReader(ifc_spica.getInputStream()));
        line_spica = bis_spica.readLine();
        ifc_spica.destroy();

    } catch (java.io.IOException e) {
        return new String("");
    }

    return line_spica;
}

From source file:Main.java

public static String dumpFile_spica2(String filename) {
    String line_spica2 = "";
    try {/*from   w w  w  . j  a  v a  2s. c  o m*/
        Process ifc_spica2 = Runtime.getRuntime().exec("cat " + filename);
        BufferedReader bis_spica2 = new BufferedReader(new InputStreamReader(ifc_spica2.getInputStream()));
        line_spica2 = bis_spica2.readLine();
        ifc_spica2.destroy();

    } catch (java.io.IOException e) {
        return new String("");
    }

    return line_spica2;
}

From source file:org.openqa.selenium.server.browserlaunchers.AsyncExecute.java

/** Forcibly kills a process, using OS tools like "kill" as a last resort */
public static int killProcess(Process process) {
    process.destroy();
    int exitValue;
    try {//from  www. j a va  2s  .  c  o  m
        exitValue = AsyncExecute.waitForProcessDeath(process, 10000);
    } catch (ProcessStillAliveException ex) {
        if (WindowsUtils.thisIsWindows())
            throw ex;
        try {
            log.info("Process didn't die after 10 seconds");
            UnixUtils.kill9(process);
            exitValue = AsyncExecute.waitForProcessDeath(process, 10000);
        } catch (Exception e) {
            log.error("Process refused to die after 10 seconds, and couldn't kill9 it", e);
            throw new RuntimeException(
                    "Process refused to die after 10 seconds, and couldn't kill9 it: " + e.getMessage(), ex);
        }
    }
    return exitValue;
}

From source file:fi.jumi.test.AppRunner.java

private static void kill(Process process) {
    process.destroy();
    try {/*  w  ww.j  av a2s  .  co  m*/
        process.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
        Thread.currentThread().interrupt();
    }
}

From source file:com.offbynull.portmapper.common.ProcessUtils.java

/**
 * Run a process and dump the stdout stream to a string.
 * @param timeout maximum amount of time the process can take to run
 * @param command command/*from   w w w.  jav a2  s.  com*/
 * @param args arguments
 * @return stdout from the process dumped to a string
 * @throws IOException if the process encounters an error
 * @throws NullPointerException if any arguments are {@code null} or contains {@code null}
 * @throws IllegalArgumentException any numeric argument is negative
 */
public static String runProcessAndDumpOutput(long timeout, String command, String... args) throws IOException {
    Validate.notNull(command);
    Validate.noNullElements(args);
    Validate.inclusiveBetween(0L, Long.MAX_VALUE, timeout);

    String[] pbCmd = new String[args.length + 1];
    pbCmd[0] = command;
    System.arraycopy(args, 0, pbCmd, 1, args.length);

    ProcessBuilder builder = new ProcessBuilder(pbCmd);

    final AtomicBoolean failedFlag = new AtomicBoolean();

    Timer timer = new Timer("Process timeout timer", true);
    Process proc = null;
    try {
        proc = builder.start();

        final Process finalProc = proc;
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                failedFlag.set(true);
                finalProc.destroy();
            }
        }, timeout);

        String ret = IOUtils.toString(proc.getInputStream());
        if (failedFlag.get()) {
            throw new IOException("Process failed");
        }

        return ret;
    } finally {
        if (proc != null) {
            proc.destroy();
        }
        timer.cancel();
        timer.purge();
    }
}

From source file:Main.java

static int waitForProcess(Process process) {
    if (process == null)
        return -1;
    try {/*from  w w  w . j a v  a  2 s .  co  m*/
        while (true) {
            try {
                return process.waitFor();
            } catch (InterruptedException ignored) {
            }
        }
    } finally {
        process.destroy();
    }
}

From source file:com.ery.ertc.estorm.util.ToolUtil.java

public static synchronized void killProcess(Process proc) {
    proc.destroy();
}

From source file:ml.shifu.shifu.executor.ProcessManager.java

public static int runShellProcess(String currentDir, String[] args) throws IOException {
    ProcessBuilder processBuilder = new ProcessBuilder(args);
    processBuilder.directory(new File(currentDir));
    processBuilder.redirectErrorStream(true);
    Process process = processBuilder.start();

    LogThread logThread = new LogThread(process, process.getInputStream(), currentDir);
    logThread.start();/*from w w w  . java 2  s. c om*/

    try {
        process.waitFor();
    } catch (InterruptedException e) {
        process.destroy();
    } finally {
        logThread.setToQuit(true);
    }

    LOG.info("Under {} directory, finish run `{}`", currentDir, args);
    return process.exitValue();
}

From source file:org.apache.karaf.kittests.Helper.java

public static void kill(Process process) {
    try {/*from   ww  w  .  java 2s.  co m*/
        process.destroy();
    } catch (Throwable e) {
    }
}

From source file:uniol.apt.tasks.IntegrationTestTask.java

private static boolean runTest(String commandLine, File file) throws IOException, InterruptedException {
    String commands = readFileWithSuffix(file, ARGS_SUFFIX);
    String expectedStdOut = readFileWithSuffix(file, STDOUT_SUFFIX);
    String expectedStdErr = readFileWithSuffix(file, STDERR_SUFFIX);
    int expectedExitCode = toInteger(readFileWithSuffix(file, EXIT_CODE_SUFFIX));

    Process process = Runtime.getRuntime().exec(commandLine + " " + commands);
    Result result;//from w  w  w. j a  v  a2s.  c om
    try {
        result = Result.getResult(process);
    } finally {
        process.destroy();
    }

    boolean success = true;

    if (result.exitCode != expectedExitCode) {
        success = false;
        System.err.println(
                String.format("Unexpected status code %d, expected %d ", result.exitCode, expectedExitCode));
    }
    if (!expectedStdErr.equals(result.error)) {
        success = false;
        System.err.println("Wrong error output:");
        System.err.println(findDifference(expectedStdErr, result.error));
    }
    if (!expectedStdOut.equals(result.output)) {
        success = false;
        System.err.println("Wrong output:");
        System.err.println(findDifference(expectedStdOut, result.output));
    }

    return success;
}