If you are working in a company which has many java applications and you do a lot of installers, you might end up in the situation where, during the install or uninstall process you might want to close a java process.
However, if you open up task manager you will only see multiple java processes.
If you want to use a normal kill process script, you will close all the java processes. But there is a solution to close only the desired java process, for example, the java process of a single application.
First, open the desired Java application (that you want to create a kill process script) and leave it open.
Next, a tool called Process Explorer from Sysinternals must be downloaded. After the download, extract the archive and run procexp64.exe.
When Process Explorer opens up, search for all java processes present on the machine.
When you find a java.exe process, right-click it and select Properties. This will open up a new window.
As you see from the screenshot above, the command line for my Java process is:
“C:\Program Files\Java\jdk-15.0.2\bin\java.exe” GetInputData
The “GetInputData” is the name of the java program i am running.
I am running another java process, and if we check the command line for that application, it is different:
For the other java application that i am running, the command line is:
“C:\Program Files\Java\jdk-15.0.2\bin\java.exe” Demo
“Demo” is the name of the Java application.
Now that we know this, we can create a script that searches all processes, and if a process has a specific string in it (for example “GetInputData”), it means that is the specific java process that we need to close.
Below the vbscript which achieves this:
On error Resume Next Dim objWMIService, objProcess, colProcess, Linie, strComputer, strList strComputer = "." Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcess = objWMIService.ExecQuery _ ("Select * from Win32_Process") For Each objProcess in colProcess if (objProcess.CommandLine <> "") Then Linie = objProcess.CommandLine if (InStr(Linie,"GetInputData")) Then objProcess.Terminate end if end if Next Set objWMIService = Nothing Set colProcess = Nothing
The only thing you need to change in the script is the name of your java application, instead of “GetInputData”, place the string that is present in the command line that you find with Process Explorer.