How can I programatically close a SPECIFIC java application running in windows from within another program?

I need to (force)close a specific java application from within another program. For any native program killing it's process or sending vm_close to it's main window usually works but not so much with java applications running on windows. Do note that the java application is only showing a tray icon and has no open window during it's normal state.

Upvotes: 1

Views: 2367

Answers (3)

user489041
user489041

Reputation: 28294

What if you wrote the process ID of the java application to a temp file in a known location. When the program starts up, do something like:


//mark the instance file for delete
String instanceFileLocation = System.getProperty("java.io.tmpdir") + File.separator + "instances.dat";
File PIDFile = new File(instanceFileLocation).deleteOnExit();
//write the process id here

Then any program or application could find the temp file and specific process id. Use the other solutions posted to kill it.

Then use

Upvotes: 0

darioo
darioo

Reputation: 47183

If you know your Java program's PID, use this:

Runtime.getRuntime().exec("taskkill /F /PID bla");

where bla is your program's PID.

That /F parameter will forcefully kill an application and I don't see a reason why a Java program would be any different from other programs and be able to survive that.

Note that this code is obviously Windows specific. If you want it to be portable, you should first find out what OS is hosting your application by using

System.getProperty("os.name")

and afterwards, use taskkill /F if you're on Windows, or kill -9 if you're on a *nix system, or something else if you're on a more exotic platform.

Update: to have a nice overview of all running processes, I'd recommend Process explorer. If you want a command line process lister, use PsList.

If you want to kill a specific Java program, it can be problematic since every Java process by default is named java.exe or javaw.exe.

Using PsList, you can programatically scan all processes which start with java and then, combined with jps, you'll have more information about what you really want to kill.

Upvotes: 3

David O'Meara
David O'Meara

Reputation: 3033

an easy hack is to copy/paste a second version of the java.exe (or javaw.exe), call it java_alt.exe and then kill the process by the new name using Unix Tools for windows

Upvotes: 0

Related Questions