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:uk.dsxt.voting.tests.TestsLauncher.java

private static void stopProcess(String name) {
    Process process = processesByName.get(name);
    if (process == null) {
        return;//w w  w  . j a  va 2  s. com
    }
    try {
        process.destroy();
        log.info("Process {} killed", name);
    } catch (Exception e) {
        log.error(String.format("Can't kill process %s", name), e);
    }
    processesByName.remove(name);
}

From source file:ca.uqac.info.Job.Launcher.JobLauncher.java

/**
 * Sets up the ProcessBuilder for the bat file and start it
 * @return The exitStatus /* w ww  . j av a 2 s  . c o  m*/
 */
private static int launchJob(String fileBat, String outName, File outFolder) throws Exception {
    // The batch file to execute
    final File batchFile = new File(fileBat);

    // The output file. All activity is written to this file
    final File outputFile = new File(outName);

    // Create the process
    final ProcessBuilder processBuilder = new ProcessBuilder(batchFile.getAbsolutePath(), outName);
    // Redirect any output (including error) to a file. This avoids deadlocks
    // when the buffers get full. 
    processBuilder.redirectErrorStream(true);
    processBuilder.redirectOutput(outputFile);

    // Add a new environment variable
    processBuilder.environment().put("JobLauncher", "Bat File Execution");

    // Set the working directory. The batch file will run as if you are in this
    // directory.
    processBuilder.directory(outFolder);

    // Start the process and wait for it to finish. 
    /* while(nextJob != true)
     {
     //Wait the end of the current Job to Launch the next one
     }
             
     nextJob = false;*/
    final Process process = processBuilder.start();
    final int exitStatus = process.waitFor();
    process.destroy();

    return exitStatus;
}

From source file:Main.java

public static void exec(File workingDir, String command) {
    try {/*from ww  w  .j a  va  2 s. c  o  m*/
        ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
        processBuilder.directory(workingDir);
        Process process = processBuilder.start();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        process.waitFor();
        process.destroy();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.neo4j.server.enterprise.ArbiterBootstrapperIT.java

private static void kill(Process process)
        throws NoSuchFieldException, IllegalAccessException, IOException, InterruptedException {
    if (SystemUtils.IS_OS_WINDOWS) {
        process.destroy();
    } else {//from   w ww. j  av a  2 s  . c  o m
        int pid = ((Number) accessible(process.getClass().getDeclaredField("pid")).get(process)).intValue();
        new ProcessBuilder("kill", "-9", "" + pid).start().waitFor();
    }
}

From source file:Main.java

private static boolean checkRootMethod3() {
    Process process = null;
    try {/* ww  w .j  a v  a2 s.  c o m*/
        process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        return in.readLine() != null;
    } catch (Throwable t) {
        return false;
    } finally {
        if (process != null)
            process.destroy();
    }
}

From source file:sd.code.stagent.services.Shell.java

public static String executeShell(String command, int waitTime) throws IOException {
    Process process = Runtime.getRuntime().exec(command);
    ProcessWithWait processWithWait = new ProcessWithWait(process);
    int exitCode = processWithWait.waitForProcess(waitTime);

    String result = "";
    if (exitCode == Integer.MIN_VALUE) {
        result = "Timeout: "; // Timeout
        process.destroy();
    } else {/*from  w  w  w  .  j av a2 s . c  om*/

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;

        while ((line = reader.readLine()) != null) {
            result = result + line + "\n";
        }
    }

    return result;
}

From source file:org.libreplan.web.print.CutyPrint.java

private static void destroy(Process process) {
    try {//from ww w  .j av a 2 s  .com
        process.destroy();
    } catch (Exception e) {
        LOG.error("error stoping process " + process, e);
    }
}

From source file:Main.java

private static boolean checkRootMethod3() {
    Process process = null;
    try {//w ww . j  a  v  a2s  .c  om
        process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        if (in.readLine() != null)
            return true;
        return false;
    } catch (Throwable t) {
        return false;
    } finally {
        if (process != null)
            process.destroy();
    }
}

From source file:Main.java

private static boolean installOrUninstallApk(String apkPath, String installOruninstall, String rOrP) {
    Process process = null;
    DataOutputStream os = null;//from  w  w w.j ava2  s .com
    String command = null;
    try {
        process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        command = "pm " + installOruninstall + " " + rOrP + " " + apkPath + " \n";
        os.writeBytes(command);
        os.flush();
        os.close();
        process.waitFor();
        process.destroy();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.atlassian.labs.bamboo.git.edu.nyu.cs.javagit.client.cli.ProcessUtilities.java

/**
 * Waits for a process to terminate and then destroys it.
 *
 * @param p//from   w w  w.ja v a2s  .c  o  m
 *          The process to wait for and destroy.
 * @return The exit value of the process. By convention, 0 indicates normal termination.
 */
public static int waitForAndDestroyProcess(Process p, IParser parser) {
    /*
     * I'm not sure this is the best way to handle waiting for a process to complete. -- jhl388
     * 06.14.2008
     */
    while (true) {
        try {
            int i = p.waitFor();
            parser.processExitCode(p.exitValue());
            p.destroy();
            return i;
        } catch (InterruptedException e) {
            // TODO: deal with this interrupted exception in a better manner. -- jhl388 06.14.2008
            continue;
        }
    }
}