0

I want to kill the particular Java process in Window with the following command line:

taskkill /f /pid <my_pid>

I didn't find a way to get the pid of my process on windows without using JNA api. I found several answers that use JNA but I'm looking for a simpler solution.

Following is the Java code that I used (which does not work):

   Field f = p.getClass().getDeclaredField("handle");
   f.setAccessible(true);
   long handle = f.getLong(p);
   System.out.println("Kill pid " + handle);
Hakim
  • 434
  • 7
  • 21
  • See also http://stackoverflow.com/questions/8435952/how-to-get-pid-from-command-line-filtered-by-username-and-imagename – Daniel Widdis Mar 09 '17 at 21:54

3 Answers3

1

Finally I found a solution. I have used wmic process get commandline, processid windows command to get the PID.

Following is my Killer.java :

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Killer {


public static void main(String[] args) throws Exception {



    ArrayList<String> cmds = new ArrayList<String>();

    cmds.add("wmic");
    cmds.add("process");
    cmds.add("get");
    cmds.add("commandline,processid");

    ProcessBuilder pb = new ProcessBuilder(cmds);

    Process p = pb.start();

    //p.waitFor();


    BufferedReader rd = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String line;
    int pid=0;

    while((line = rd.readLine()) != null)
    {
        if(line.contains(args[0]) && !line.contains("Killer"))
        {

            System.out.println("OK" + line);
            String[] split = line.split(" ");
            pid=Integer.parseInt(split[split.length - 1]);

        }
        else
        {
            //System.out.println("  " + line);
        }
    }

    cmds = new ArrayList<String>();

    System.out.println("Kill pid " + pid);
    cmds.add("taskkill");
    cmds.add("/T");
    cmds.add("/F");
    cmds.add("/PID");
    cmds.add("" + pid);
    pb = new ProcessBuilder(cmds);
    pb.start();
 }               
}

Hope it will help you.

Hakim
  • 434
  • 7
  • 21
0

As you discovered, you can use WMI through the command-line wmic, but you can also use the tasklist command.

It takes several switches to filter the output.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
  • But how would you know which process from the list you have to kill ? – Hakim Mar 10 '17 at 08:44
  • The same way you would with WMI, you search by ImageName (the program, like `tasklist.exe`) or other fields. I posted a specific example in a comment to your original post which shows the syntax. While `tasklist` will just list it, you can use similar syntax (slightly different switches) to filter directly with the `taskkill` command. You don't even have to match the PID if you match other fields. – Daniel Widdis Mar 10 '17 at 17:23
0

Since the question has jna in the tags, here's also a JNA-compatible approach...

int pid;
try {
    pid = Kernel32.INSTANCE.GetCurrentProcessId();
}
catch(UnsatisfiedLinkError | NoClassDefFoundError e) {
    log.warn("Could not obtain process ID.  This usually means JNA isn't working.  Returning -1.");
    pid = -1;
}

... or for Unix:

private interface CLibrary extends Library {
    CLibrary INSTANCE = Native.load("c", CLibrary.class);
    int getpid();
}

int pid;
try {
    pid = CLibrary.INSTANCE.getpid();
}
catch(UnsatisfiedLinkError | NoClassDefFoundError e) {
    log.warn("Could not obtain process ID.  This usually means JNA isn't working.  Returning -1.");
    pid = -1;
}
tresf
  • 7,103
  • 6
  • 40
  • 101