【问题标题】:How to kill a process in Java process.destroy()如何在 Java process.destroy() 中杀死一个进程
【发布时间】:2013-11-12 15:45:08
【问题描述】:

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

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

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

编辑:

我找到了一种方法。在进程开始时获取进程 ID。

【问题讨论】:

  • 您是否可以直接从您的应用程序(通过ProcessBuilderRuntime.exec())运行jar,而不是使用bat 文件来执行此操作?如果是这样,那么您只需拨打destroy() 就可以了。
  • dic19 所说的应该有效。我看到的问题是您正在创建一个流程来创建一个流程。我没有看到任何直接的方法来处理第二个过程。可以,但是去掉中间人,直接启动第二个流程会简单很多。那,你目前正在做的应该工作。
  • 是的,但我正在通过 bat 文件调用两个进程。
  • 回滚编辑,因为某些更改不正确,除非通过“cmd”,OP 实际上意味着“命令”。这是模棱两可的 - 第一次它似乎是“命令”的缩写,但第二个似乎是指 cmd。请说清楚。 (不管怎样,taskkill 是一个实用程序的名称,不应该被分成两个词。)
  • 顺便说一句,如果您找到了解决方案,请将其发布为答案而不是编辑,并提供更多详细信息,以便对未来的读者有用。

标签: java process cmd pid taskkill


【解决方案1】:

我改程序直接启动程序(第二个进程)。

我使用以下方法破坏了进程

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

【讨论】:

    猜你喜欢
    • 2011-09-27
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多