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:com.asakusafw.testdriver.DefaultJobExecutor.java

private int runCommand(List<String> commandLine, Map<String, String> environmentVariables) throws IOException {
    LOG.info(MessageFormat.format(Messages.getString("DefaultJobExecutor.infoEchoCommandLine"), //$NON-NLS-1$
            toCommandLineString(commandLine)));

    ProcessBuilder builder = new ProcessBuilder(commandLine);
    builder.redirectErrorStream(true);/*from  w w  w  .j a va 2s .  c  o  m*/
    builder.environment().putAll(environmentVariables);
    File hadoopCommand = configurations.getHadoopCommand();
    if (hadoopCommand != null) {
        builder.environment().put("HADOOP_CMD", hadoopCommand.getAbsolutePath()); //$NON-NLS-1$
    }
    builder.directory(new File(System.getProperty("user.home", "."))); //$NON-NLS-1$ //$NON-NLS-2$

    int exitCode;
    Process process = builder.start();
    try (InputStream is = process.getInputStream()) {
        InputStreamThread it = new InputStreamThread(is);
        it.start();
        exitCode = process.waitFor();
        it.join();
    } catch (InterruptedException e) {
        throw new IOException(
                MessageFormat.format(Messages.getString("DefaultJobExecutor.errorExecutionInterrupted"), //$NON-NLS-1$
                        toCommandLineString(commandLine)),
                e);
    } finally {
        process.getOutputStream().close();
        process.getErrorStream().close();
        process.destroy();
    }
    return exitCode;
}

From source file:org.apache.hadoop.hbase.allocation.group.MoveConfImpl.java

/**
 * Start or stop regionserver from master use shell script
 * //w w w .j  av  a  2 s  .  c  o  m
 * @param server
 *          regionserver name ,example "dw83.kgb.sqa.cm4,60020"
 * @param command
 *          "start" or "stop" ,means start or stop regionserver
 * @return true if success ,else false
 */
public boolean ImplRegionServer(String server, String command) {
    Process process = null;
    if (!command.equals("start") && !command.equals("stop")) {
        LOG.info("This shell script only support start and stop command.");
        return false;
    }
    String[] cmd = new String[4];
    cmd[0] = currentdir + "/restartserver.sh";
    cmd[1] = server;
    cmd[2] = HbaseDIR;
    cmd[3] = command;
    try {
        process = Runtime.getRuntime().exec(cmd, null, new File(currentdir));
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    try {
        process.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return false;
    }
    process.destroy();
    return true;
}

From source file:com.hpe.application.automation.tools.srf.run.RunFromSrfBuilder.java

private void cleanUp() {
    if (eventSrc != null) {
        eventSrc.close();//  w w  w  .j av  a2 s  .com
        eventSrc = null;
    }

    if (_con != null) {
        _con.disconnect();
        _con = null;
    }

    if (srfCloseTunnel && CreateTunnelBuilder.Tunnels != null) {
        for (Process p : CreateTunnelBuilder.Tunnels) {
            p.destroy();
        }
        CreateTunnelBuilder.Tunnels.clear();
    }
}

From source file:org.sonatype.flexmojos.tests.AbstractFlexMojosTests.java

protected void assertSeftExit(File main, int expectedExitCode) throws Exception {
    Process p = null;
    try {/*from  w  ww.ja  va2s  .c  o  m*/
        p = Runtime.getRuntime().exec(new String[] { "flashplayer", main.getCanonicalPath() });
        final Process tp = p;

        Thread t = new Thread(new Runnable() {

            public void run() {
                try {
                    tp.waitFor();
                } catch (InterruptedException e) {
                }
            }
        });

        t.start();

        t.join(10000);

        MatcherAssert.assertThat(p.exitValue(), CoreMatchers.equalTo(expectedExitCode));
    } finally {
        if (p != null)
            p.destroy();
    }
}

From source file:org.rhq.maven.plugins.ValidateMojo.java

private void validate(File agentPluginArchive, Set<File> parentPlugins) throws MojoExecutionException {
    JarFile jarFile = null;/*from   w w w  .j  a  v  a  2  s. c om*/
    try {
        jarFile = new JarFile(agentPluginArchive);

        if (jarFile.getEntry("META-INF/rhq-plugin.xml") == null) {
            handleFailure("Descriptor missing");
        }
    } catch (Exception e) {
        handleException(e);
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Exception e) {
                handleException(e);
            }
        }

    }

    // Run the plugin validator as a forked process
    Process process = null;
    try {
        String pluginValidatorClasspath = buildPluginValidatorClasspath(agentPluginArchive, parentPlugins);
        String javaCommand = buildJavaCommand();
        ProcessBuilder processBuilder = buildProcessBuilder(javaCommand, pluginValidatorClasspath);
        process = processBuilder.start();
        redirectOuput(process);
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            handleFailure("Invalid plugin");
        }
    } catch (Exception e) {
        handleException(e);
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
}

From source file:org.mule.util.SystemUtils.java

private static Map getenvJDK14() throws Exception {
    Map env = new HashMap();
    Process process = null;

    try {//from  w  w w  .  jav  a  2  s . co m
        boolean isUnix = true;
        String command;

        if (SystemUtils.IS_OS_WINDOWS) {
            command = "cmd /c set";
            isUnix = false;
        } else {
            command = "env";
        }

        process = Runtime.getRuntime().exec(command);
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = br.readLine()) != null) {
            for (int prefix = 0; prefix < UNIX_ENV_PREFIXES.length; prefix++) {
                if (line.startsWith(UNIX_ENV_PREFIXES[prefix])) {
                    line = line.substring(UNIX_ENV_PREFIXES[prefix].length());
                }
            }

            int index = -1;
            if ((index = line.indexOf('=')) > -1) {
                String key = line.substring(0, index).trim();
                String value = line.substring(index + 1).trim();
                // remove quotes, if any
                if (isUnix && value.length() > 1 && (value.startsWith("\"") || value.startsWith("'"))) {
                    value = value.substring(1, value.length() - 1);
                }
                env.put(key, value);
            } else {
                env.put(line, StringUtils.EMPTY);
            }
        }
    } catch (Exception e) {
        throw e; // bubble up
    } finally {
        if (process != null) {
            process.destroy();
        }
    }

    return env;
}

From source file:com.google.dart.server.internal.remote.StdioServerSocket.java

/**
 * Wait up to 5 seconds for process to gracefully exit, then forcibly terminate the process if it
 * is still running.//  w w w .  ja v  a  2s. com
 */
@Override
public void stop() {
    if (process == null) {
        return;
    }
    final Process processToStop = process;
    process = null;
    long endTime = System.currentTimeMillis() + 5000;
    while (System.currentTimeMillis() < endTime) {
        try {
            int exit = processToStop.exitValue();
            if (exit != 0) {
                Logging.getLogger()
                        .logInformation("Non-zero exit code: " + exit + " for\n   " + analysisServerPath);
            }
            return;
        } catch (IllegalThreadStateException e) {
            //$FALL-THROUGH$
        }
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            //$FALL-THROUGH$
        }
    }
    processToStop.destroy();
    Logging.getLogger().logInformation("Terminated " + analysisServerPath);
}

From source file:com.asakusafw.operation.tools.hadoop.fs.CleanTest.java

private File link(String path, File target, int day) throws IOException {
    Assume.assumeFalse("In Windows, tests with symlink are skipped", SystemUtils.IS_OS_WINDOWS);
    File link = file(path);/*from  w  ww.j a  va2 s  .  c o m*/
    link.getParentFile().mkdirs();
    try {
        Process process = new ProcessBuilder()
                .command("ln", "-s", target.getCanonicalPath(), link.getAbsolutePath())
                .redirectErrorStream(true).start();
        try {
            int exit = process.waitFor();
            Assume.assumeThat(exit, is(0));
        } finally {
            process.destroy();
        }
    } catch (Exception e) {
        Assume.assumeNoException(e);
    }
    touch(path, day);
    return link;
}

From source file:com.doctoror.surprise.SurpriseService.java

private static Result execute(final List<String> commands, final boolean surpriseBinary) {
    final Result result = new Result();
    Process process = null;
    DataOutputStream os = null;//from w ww . j  a v  a 2  s .c  o m
    BufferedReader is = null;
    try {
        process = new ProcessBuilder().command(surpriseBinary ? COMMAND_SURPRISE : COMMAND_SU)
                .redirectErrorStream(true).start();
        os = new DataOutputStream(process.getOutputStream());
        is = new BufferedReader(new InputStreamReader(process.getInputStream()));
        for (final String command : commands) {
            os.writeBytes(command + "\n");
        }
        os.flush();

        os.writeBytes("exit\n");
        os.flush();

        final StringBuilder output = new StringBuilder();
        String line;
        try {
            while ((line = is.readLine()) != null) {
                if (output.length() != 0) {
                    output.append('\n');
                }
                output.append(line);
            }
        } catch (EOFException ignored) {
        }

        result.output = output.toString();
        result.exitCode = process.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
        result.exitCode = -666;
        result.output = e.getMessage();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ignored) {
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignored) {
            }
        }
        if (process != null) {
            try {
                process.destroy();
            } catch (Exception ignored) {
            }
        }
    }
    return result;
}

From source file:org.rhq.maven.plugins.ExecCliCommandMojo.java

@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException {
    Process process = null;
    try {//from   w  w  w .jav  a  2s  . co m
        File rhqCliStartScriptFile = getRhqCliStartScriptFile();
        // Run the CLI in forked process
        ProcessBuilder processBuilder = new ProcessBuilder() //
                .directory(rhqCliStartScriptFile.getParentFile()) // bin directory
                .command(buildRhqCliCommand(rhqCliStartScriptFile));
        getLog().info("Executing RHQ CLI command: " + IOUtils.LINE_SEPARATOR + command);
        process = processBuilder.start();
        redirectOuput(process);
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            handleFailure("CLI stopped with status code: " + exitCode);
        }
    } catch (Exception e) {
        handleException(e);
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
}