In Linux when I run the destroy function on java.lang.Process object (Which is true typed java.lang.UNIXProcess ) it sends a SIGTERM signal to process, is there a way to kill it with SIGKILL?
4 Answers
Not using pure Java.
Your simplest alternative is to use Runtime.exec()
to run a kill -9 <pid>
command as an external process.
Unfortunately, it is not that simple to get hold of the PID. You will either need to use reflection black-magic to access the private int pid
field, or mess around with the output from the ps
command.
UPDATE - actually, there is another way. Create a little utility (C program, shell script, whatever) that will run the real external application. Code the utility so that it remembers the PID of the child process, and sets up a signal handler for SIGTERM that will SIGKILL the child process.

- 698,415
- 94
- 811
- 1,216
-
I can do it like this or in JNI , although I am not eager to do it this way, how do you know the pid of the process you want to kill? – ekeren Jun 01 '10 at 14:12
-
First thanks for these replies. reflection and JNI/exec are my last resort, I wonder if someone can find a more elegant way to do this. – ekeren Jun 01 '10 at 14:20
-
I also tough about a wrapper executioner, actually my boss tough about it:), So I understand that there is no straight forward way of doing this that you know of. – ekeren Jun 01 '10 at 14:25
-
@ekeren - Correct. I suppose you could download the OpenJDK sources, modify them and build a custom JVM ... but that's an even worse alternative. – Stephen C Jun 01 '10 at 14:34
-
1If you're going to make system library calls, don't do it straight up in JNI...save yourself from some headaches and do it in JNA instead. – rob Oct 18 '13 at 23:26
-
Don't know if it's portable, but on Android `Process.toString()` yields string `Process[pid=1234]` – Alexander Malakhov Feb 13 '14 at 07:32
-
Though I feel uncomfortable with this and in my code I opted to "mess around with the output from the ps" :) – Alexander Malakhov Feb 13 '14 at 07:38
-
1@AlexanderMalakhov - No it is not portable, since the javadoc for the `toString()` method doesn't specify the format of the String. According to the source code, for Oracle Java on Unix, the `toString()` method on a `Process` and `UnixProcess` is not overridden from `Object.toString()`. What you are seeing is (at best) Android specific. – Stephen C Feb 13 '14 at 07:46
-
Have a look at [this post](http://stackoverflow.com/a/31570187/2780808), if you can use Java 1.8 – Ercksen Aug 08 '15 at 10:04
-
@Ercksen -Have you checked the native source code to see if "forcibly" really means SIGTERM? – Stephen C Aug 08 '15 at 22:21
-
Have a look at [this](http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/ff0da4ea08a2), and at point `5.21` you can see how the signal is determined: `int sig = (force == JNI_TRUE) ? SIGKILL : SIGTERM;`. So as you can see, SIGTERM is the default signal, and SIGKILL is used when forced. Please note that this is only for the `UNIXProcess` class, but I believe that other systems would not behave different. – Ercksen Aug 08 '15 at 22:50
-
Is there any reason that this should work for `kill -9` but not work for `kill -2`? I'm finding that `kill -2` doesn't work when called from java, but does work from a shell. Details: https://stackoverflow.com/q/49522487/1054322 – MatrixManAtYrService Mar 27 '18 at 21:34
-
That question makes little sense to me. For a start, the code that supposedly shows you doing a `kill -2` from Java doesn't do that. And the commented out code won't compile because there is no pid attribute on `Process`. – Stephen C Mar 28 '18 at 03:20
Stephen his answer is correct. I wrote what he said:
public static int getUnixPID(Process process) throws Exception
{
System.out.println(process.getClass().getName());
if (process.getClass().getName().equals("java.lang.UNIXProcess"))
{
Class cl = process.getClass();
Field field = cl.getDeclaredField("pid");
field.setAccessible(true);
Object pidObject = field.get(process);
return (Integer) pidObject;
} else
{
throw new IllegalArgumentException("Needs to be a UNIXProcess");
}
}
public static int killUnixProcess(Process process) throws Exception
{
int pid = getUnixPID(process);
return Runtime.getRuntime().exec("kill " + pid).waitFor();
}
You can also get the pid this way:
public static int getPID() {
String tmp = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
tmp = tmp.split("@")[0];
return Integer.valueOf(tmp);
}

- 67,591
- 47
- 198
- 287
-
on linux, i run commands through java (interactive process): nslookup -> google, this hangs the process, and i can not kill this process. So now i used your suggestion, but it does not terminate this process, it just hangs and i can not terminate my program. I even tried to kill the pid manually but no avail, what should i do??? – Space Rocker Mar 15 '13 at 13:05
-
@Space Rocker: a simple kill sends a SIGTERM. A "kill -9" sends a SIGKILL. Try "kill -9 -1": that sends SIGKILL to all process in the process group which pid > 1. If that doesn't kill it than nothing will. But this will work. – Csaba Toth Apr 09 '13 at 23:48
-
Definitively use the second way to get self PID. VisualVM does the same. The former way is ugly hack, it is not cross platform, and may not working in future Java versions. – Espinosa Jul 03 '13 at 17:14
-
@Espinosa - The 2nd way is not portable either. The javadoc for `getName` says: *"The returned name string can be any arbitrary string and a Java virtual machine implementation can choose to embed platform-specific useful information in the returned name string."* Note: **can** choose, means that it can also chose not to. And there is no spec of how that information should be formatted. – Stephen C Feb 13 '14 at 07:53
-
As of Java 9, you can just call `process.pid()` to get the process id. – Luke Hutchison Dec 02 '20 at 14:02
If you know process name you can use pkill
Runtime.getRuntime().exec("pkill firefox").waitFor();

- 1,642
- 4
- 24
- 38
Since Java 1.8
you can call the method destroyForcibly()
, which calls the destroy()
method by default, but according to the Java docs, all sub-processes returned by ProcessBuilder
or Runtime.exec()
implement this method.

- 668
- 9
- 23
-
1Unfortunately currently the implementation simply calls `destroy()`. See here: https://bugs.openjdk.java.net/browse/JDK-8056139 – Joe Jan 26 '16 at 15:33