Take the 2-minute tour ×

Is it possible to stop an application in windows using java code ? I'd like to create a program which is like a shortcut to stop an application on my pc. Is it possible using ProcessBuilder ? Thank you

share|improve this question
I've done it in the past by wrapping calls to the pskill Windows util (don't know if that one is still around) using Runtime.exec. However I don't know which guaranteed the OS makes regarding pskill. AFAICT on Windows it's much more different than on Unx. On Unx issuing a SIGKILL means that the program must exit immediately and that all its resources must be given back to the OS (it's the theory and it works quite well in practice). –  TacticalCoder Dec 6 '11 at 1:50
TerminateProcess and NtTerminateProcess are analogous to sending SIGKILL in NT - that is, it requests the OS to terminate the process "immediately," although (for Linux and Windows, and probably most *nix) this may be deferred due to the process accessing certain system resources (such as an IO call that never returns). The analogue to SIGTERM in Windows is to send a WM_CLOSE window message to a window of the application, unless the application is a service. In that case, use ControlService with dwControl = SERVICE_CONTROL_STOP. –  user314104 Dec 6 '11 at 2:33

3 Answers 3

Plain Java, without native access? No.

ProcessBuilder lets you control processes that you started from within the Java application, but it doesn't give you control to processes that were started by other processes.

As per user988052's comment, you can use pskill from the Sysinternals Suite to accomplish this through Runtime.exec. pskill uses the TerminateProcess function call. You can also call this function with JNI (or some other native caller, like JNA, NLink, etc.), but you'll need to obtain a handle to the process via the OpenProcess function call.

share|improve this answer
If you use OpenProcess, be sure you request PROCESS_TERMINATE (0x0001) in dwDesiredAccess. –  user314104 Dec 6 '11 at 2:27

I believe the JRE does not provide an API for system processes. ProcessBuilder can create system processes, but it cannot provide you with a Process object for a process created outside the JVM. I believe that is a philosophical decision on the part of the Java folks, to avoid certain OS-specific tasks where possible. You might think that every OS has pids, or that they all have some kind of kill message, but Java runs on lots of OSes, some of them quite strange.

You could make native code to do it and wrap that with JNI, but that would be wasteful. As Mr 988052 says, I suggest you execute a system-specific command through Runtime.exec or ProcessBuilder. You would need to decide which OSes you want to support, and be sure to get the commands (and the OS-detection code) right for each one.

share|improve this answer

Is it possible to stop an application in windows using java code ? ... Is it possible using ProcessBuilder ?

Only if you launched the application from the same Java application that you are trying to fill it from.

Other than that, you need to resort to running a Windows-specific command to do the killing ... or something based on JNI to make native Windows library calls.

The other complications are that you may not be able to kill certain processes due to permissions issues, and some process may refuse to be killed.

share|improve this answer

Your Answer


