I want to kill the particular Java process in Windows, like in Linux (ps -aux
to get processid and then kill processid
to kill the process).
The solution I found is very simple. Use Window's WMIC & Java's Runtime to locate & kill the process.
Part 1: You need to put some sort of identifier into your app's startup command line. E.g. something like:
String id = "com.domain.app";
Part 2: When you run your app, make sure to include the string. Let's say you start it from within Java, do the following:
Runtime.getRuntime().exec(
"C:\...\javaw.exe -cp ... -Dwhatever=" + id + " com.domain.app.Main"
);
Part 3: To kill the process, use Window's WMIC. Just make sure you app was started containing your id from above:
Runtime.getRuntime().exec(
"wmic process Where \"CommandLine Like '%" + id + "%'\" Call Terminate"
);
ps -ef | grep java
pid
of running jdkkill -9 [pid]