이동준
이동준

Reputation: 21

How to kill a java process (by name) in Windows' cmd.exe?

When I run jps -lv, I get

4748 org.apache.abc.runtime.common.abc -XX:PermSize=128m -XX:MaxPermSize=128m -Xmx512m -ea -Dproc_abc 11140 sun.tools.jps.Jps -Dapplication.home=C:\Program Files\Java\jdk1.7.0_79 -Xms8m

I need windows batch command to kill abc.

In linux,

taskkill /f /pid $(jps -lv   | grep abc   | awk '{print $1}')

this would be work but in windows, I can't not find how to do it.

Please help me.

Upvotes: 2

Views: 5511

Answers (2)

Hackoo
Hackoo

Reputation: 18827

You can try it in batch like this :

@echo off
for /f "delims= " %%a in ('jps -lv ^| find /i "common"') do set PID=%%a
taskkill /f /PID %PID%
pause

Or like this way :

@echo off
for /f "tokens=1" %%A in ('jps -lv ^| find "common"') do (taskkill /F /PID %%A)
pause

Upvotes: 3

Richard
Richard

Reputation: 108975

Take a look at pskill from SysInternals.

Usage: pskill [-t] [\\computer [-u username [-p password]]] 
     -t    Kill the process and its descendants.
     -u    Specifies optional user name for login to
           remote computer.
     -p    Specifies optional password for user name. If you omit this
           you will be prompted to enter a hidden password.

But for as more contemporary approach in Windows you would be better off with PowerShell and WMI.

Eg. To kill a FireFox process with "default" on the command line I could use:

(gwmi win32_process -filter 'name = "firefox.exe" and commandline like "%default%"').Handle | % { stop-process $_ }

gwmi is short for Get-WmiObject, the Win32_Process object uses its Handle property to hold the process id. % is an alias for ForEach-Object and executes the passed script block. $_ is the current object being passed down the pipe,

Upvotes: 1

Related Questions