How to stop process started with an Eclipse external tools configuration
You should use this command TASKKILL
Syntax TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter] [/PID processid | /IM imagename] } [/F] [/T]
Options /S system The remote system to connect to.
/U [domain\]user The user context under which the command should execute. /P [password] The password. Prompts for input if omitted. /F Forcefully terminate the process(es). /FI filter Display a set of tasks that match a given criteria specified by the filter. /PID process id The PID of the process to be terminated. /IM image name The image name of the process to be terminated. Wildcard '*' can be used to specify all image names. /T Tree kill: terminates the specified process and any child processes which were started by it.
Filters Apply one of the Filters below:
Imagename eq, ne String PID eq, ne, gt, lt, ge, le Positive integer. Session eq, ne, gt, lt, ge, le Any valid session number. Status eq, ne RUNNING | NOT RESPONDING CPUTime eq, ne, gt, lt, ge, le Time hh:mm:ss MemUsage eq, ne, gt, lt, ge, le Any valid integer. Username eq, ne User name ([Domain\]User). Services eq, ne String The service name Windowtitle eq, ne String Modules eq, ne String The DLL name
Examples:
TASKKILL /S system /F /IM notepad.exe /T TASKKILL /PID 1230 /PID 1241 /PID 1253 /T TASKKILL /F /IM notepad.exe /IM mspaint.exe TASKKILL /F /FI "PID ge 1000" /FI "WINDOWTITLE ne untitle*" TASKKILL /F /FI "USERNAME eq NT AUTHORITY\SYSTEM" /IM notepad.exe TASKKILL /S system /U domain\username /FI "USERNAME ne NT*" /IM * TASKKILL /S system /U username /P password /FI "IMAGENAME eq note*"
This is an example named : ProcessKiller.bat :
To kill differents process like eclipse.exe
and javaw.exe
at once and log the result into a logfile for success or failure !
@echo off
cls & color 0A
Mode con cols=50 lines=6
Title ProcessKiller by Hackoo 2016
set process="eclipse.exe" "javaw.exe"
set Tmp=Tmp.txt
set LogFile=ProcessKillerLog.txt
If Exist %Tmp% Del %Tmp%
If Exist %LogFile% Del %LogFile%
For %%a in (%process%) Do Call :KillMyProcess %%a %Tmp%
Cmd /U /C Type %Tmp% > %LogFile%
If Exist %Tmp% Del %Tmp%
Start "" %LogFile%
Exit /b
:KillMyProcess
Cls
echo.
ECHO **************************************
Echo Trying to kill "%~1"
ECHO **************************************
(
Echo The Process : "%~1"
Taskkill /IM "%~1" /F /T
Echo =======================
)>>%2 2>&1
If you want to kill all java.exe
processes : Taskkill /F /IM java.exe /T
or wmic process where "name like '%java%'" delete
The best workaround I found so far is a reusable external applications launcher:
import java.lang.ProcessBuilder.Redirect;
public class Main {
public static void main(String[] args) throws Exception {
Process process = new ProcessBuilder(args[0])
.redirectOutput(Redirect.INHERIT)
.redirectError(Redirect.INHERIT)
.start();
Thread thread = new Thread(() -> readInput(args[1]));
thread.setDaemon(true);
thread.start();
process.waitFor();
}
private static void readInput(String commandLinePart) {
try {
while (System.in.read() != -1);
killProcess(commandLinePart);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void killProcess(String commandLinePart) throws Exception {
final String space = " ";
String[] commandLine = "wmic process where \"commandLine like '%placeholder%'\" delete"
.replaceAll("placeholder", commandLinePart).split(space);
new ProcessBuilder(commandLine).start();
}
}
The idea is to start this application instead of the external one and pass it the information about the target application as command line arguments.
The launcher application then starts the process, redirects the output and error streams (so that I see the target application output in the Eclipse console), waits until the target process is completed and waits for EOF from the standard input.
The last point actually does the trick: When I kill the process from the Eclipse console, the standard input reaches EOF, and the launcher application knows that it's time to stop the target process as well.
The Eclipse External Tools Configuration dialog looks now like this:
Location
is always the same for all configurations and points to the start.bat
file that simply runs the launcher application:
java -jar C:\ExternalProcessManager\ExternalProcessManager.jar %1 %2
It also takes the two command line arguments:
- The target process (in my case
test.bat
which simply starts my test application:java -jar Test.jar
). - The part of command line used to start the application (in my case
Test.jar
), so that launcher application can uniquely identify and kill the target process when I terminate the process from Eclipse console.