简体   繁体   中英

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.

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.

一个简单的技巧是复制/粘贴java.exe(或javaw.exe)的第二个版本,将其称为java_alt.exe,然后使用Windows的Unix工具以新名称杀死该进程。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM