java - 如何在 Java process.destroy() 中终止进程

标签 java process cmd pid taskkill

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

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

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

编辑:

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

最佳答案

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

我用下面的来破坏进程

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

关于java - 如何在 Java process.destroy() 中终止进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19726804/

相关文章:

java - 如何在 Eclipse Keplar 上安装 Lotus Expeditor

java - 为什么我得到 org.hibernate.MappingException : Could not instantiate id generator?

java - 有哪些工具可以分析 Java 中堆外的内存使用情况?

windows - 监控 Windows 中的进程

batch-file - 如何将参数传递给 PuTTY 命令文件

windows - 是否可以在 CALL 命令中传递未处理的参数?

java - 使用处理程序将重复任务延迟有限次数

linux - linux调度进程还是线程?

c# - 关于 C# 进程类 :I can start some processes but another can not open

node.js - 删除 Windows 10 上的所有文件夹