如何在Java process.destroy()中杀死进程

时间:2013-11-01 12:30:40

标签: java process cmd pid taskkill

我厌倦了使用process.destroy();杀死进程的方法。经过一些研究后,我了解到它有时不起作用,所以我尝试使用“Taskkiller”杀死任务。

使用此: Java tool/method to force-kill a child process

我正在运行一个cmd,我正在通过cmd(bat文件)调用一个jar。 我可以通过taskkill停止cmd。但我找不到阻止罐子的方法。

编辑:

我找到了办法。在流程开始时获取流程ID。

1 个答案:

答案 0 :(得分:0)

我改变了程序直接启动程序(第二个过程)。

并且我使用以下内容来销毁进程

private static void destroyProcess(final Process process) {

    if (process != null) {

        Field field;
        final Runtime runtime = Runtime.getRuntime();

        final Class<? extends Process> getClass = process.getClass();
        if (JAVA_LANG_UNIX_PROCESS.equals(getClass.getName())) {
            // get the PID on unix/linux systems
            try {
                field = getClass.getDeclaredField("pid");
                field.setAccessible(true);
                final Object processID = field.get(process);
                final int pid = (Integer) processID;
                // killing the task.

                    runtime.exec("kill -9 " + pid);
                } catch (IOException e) {
                    LOGGER.error("Error in Killing the process:" + e);
                } catch (SecurityException e) {
                    LOGGER.error("Error in Killing the process:" + e);
                } catch (NoSuchFieldException e) {
                    LOGGER.error("Error in Killing the process:" + e);
                } catch (IllegalArgumentException e) {
                    LOGGER.error("Error in Killing the process:" + e);
                } catch (IllegalAccessException e) {
                    LOGGER.error("Error in Killing the process:" + e);
                }
        }

        final String classGetName = getClass.getName();
        if (JAVA_LANG_WIN32_PROCESS.equals(classGetName) || JAVA_LANG_PROCESS_IMPL.equals(getClass.getName())) {
            // determine the pid on windowsplattforms
            process.destroy();
            try {
                field = getClass.getDeclaredField("handle");
                field.setAccessible(true);
                final int pid = Kernel32.INSTANCE.GetProcessId((Long) field.get(process));
                // killing the task.
                runtime.exec("taskkill " + pid);

            } catch (SecurityException e) {
                LOGGER.error("Error in Killing the process:" + e);
            } catch (NoSuchFieldException e) {
                LOGGER.error("Error in Killing the process:" + e);
            } catch (IOException e) {
                LOGGER.error("Error in Killing the process:" + e);
            } catch (IllegalArgumentException e) {
                LOGGER.error("Error in Killing the process:" + e);
            } catch (IllegalAccessException e) {
                LOGGER.error("Error in Killing the process:" + e);
            }

        }
    }
}

/**
 * 
 * This interface use to kernel32.
 */
static interface Kernel32 extends Library {

    /**
     *
     */
    Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);

    /**
     * 
     * GetProcessId.
     * 
     * @param hProcess
     *            hProcess
     * @return return the PID.
     */
    int GetProcessId(Long hProcess);
    // NOTE : Do not change the GetProcessId method name.
}

我从以下链接获得了帮助:http://www.golesny.de/p/code/javagetpid