How to stop a process started by setting up external Eclipse tools

I have a windows .bat file that runs a java program. For convenience, I created a setup for external Eclipse tools to run it directly from the IDE and read its standard output from the Eclipse console.

However, when I complete the process from Eclipse using the completion button (red square) in the Console view, the program still works.

How to kill it from Eclipse (without creating a separate launch configuration that will look for it and kill it programmatically)?

+5
source share
2 answers

The best workaround I have found so far is a multitask application to run external applications:

 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 run this application instead of the external one and pass it information about the target application as command line arguments.

Then the launching application starts the process, redirects the output and error flows (so I see the output of the target application in the Eclipse console), waits for the target process to complete, and waits for EOF input from standard input.

The last point actually does the trick: when I kill a process from the Eclipse console, standard input reaches EOF, and the application starts that it also stops the target process.

The Customize Eclipse External Tools dialog box looks like this:

enter image description here

Location always the same for all configurations and points to the start.bat file, which simply launches the launch application:

java -jar C:\ExternalProcessManager\ExternalProcessManager.jar %1 %2

It also accepts two command line arguments:

  • The target process (in my case test.bat , which just launches my test application: java -jar Test.jar ).
  • The part of the command line used to launch the application (in my case Test.jar ), so that the launch application can uniquely identify and kill the target process when I end the process from the Eclipse console.
+2
source

You must use this TASKKILL command

Syntax TASKKILL [/ S system [/ U username [/ P [password]]]] {[/ FI filter] [/ PID processid | / IM imagename]} [/ F] [/ T]

Options / S Remote system to connect.

 /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 following filters:

  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 with the name: ProcessKiller.bat :

To kill different processes, such as eclipse.exe and javaw.exe , and put the result in a log file 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

+3
source

Source: https://habr.com/ru/post/1244452/


All Articles