4

I tired using process.destroy(); method to kill a process. After some research I learned that it won't work sometimes, so I tried killing the task using "Taskkiller".

Using this: Java tool/method to force-kill a child process

I'm running a cmd through the process and I'm calling a jar through cmd (bat file). I can stop the cmd through taskkill. But I couldn't find a way to stop the jar.

EDIT:

I found a way to do it. Getting the Process ID at the beginning of a Process.

Community
  • 1
  • 1
Rane
  • 81
  • 3
  • 8
  • Are you able to run the `jar` directly from your app (through `ProcessBuilder` or `Runtime.exec()`) instead using a bat file to do so? If so then you can just call `destroy()` and it should work. – dic19 Nov 01 '13 at 12:43
  • What dic19 said should work. The problem I see is that you are creating a process to create a process. I don't see any straight-forward way to get a handle to the second process. It can be done, but it would be much simpler to eliminate the middle-man and launch the second process directly. That, what you are currently doing should work. – MadConan Nov 01 '13 at 12:53
  • Yeah true, but i m calling two processes through the bat file. – Rane Nov 02 '13 at 04:00
  • Rolled back the edits because some of the changes were incorrect, unless by "cmd" the OP actually means "command". It's ambiguous - the first time it appears to be short for "command", but the second one appears to refer to cmd. Please clarify. (Either way, taskkill is the name of a utility, and should not have been split into two words.) – Adi Inbar Jan 31 '14 at 15:46
  • 3
    BTW, if you found a solution, please post it as an answer rather than an edit, and provide more detail so that it will be useful to future readers. – Adi Inbar Jan 31 '14 at 15:48
  • @Rane hi, have you considered doing what `Adi Inbar` has suggested? – Joe DF Mar 24 '14 at 05:52

1 Answers1

0

I changed the program to launch the program(the second process) directly.

and i used the following to destroy the process

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.
}

i got help from this following link : http://www.golesny.de/p/code/javagetpid

Rane
  • 81
  • 3
  • 8