How can we stop a running java process through Windows cmd?
Asked Answered
E

11

46

I am a newbie in cmd, so please allow me to ask a stupid question: How can we stop a running Java process through Windows cmd?

For example, if we start Jetty (a mini web server) with the following command:

start javaw -jar start.jar

How do we find the process and stop it later?

Obviously the following command does not work:

stop javaw -jar start.jar
Eleventh answered 15/4, 2010 at 8:54 Comment(5)
Did you try Ctrl + C or Ctrl + Break?Diley
yes, but can I build a .bat file to stop it with Ctrl + C or Ctrl + Break?Eleventh
Maybe check the taskkill command. It has many options for choosing the process to kill: by process id, by name pattern, by owning user, etc. But I recommend first looking for a standard, less violent way of closing the specific application (servers usually have some kind of "stop" command)Bathysphere
Do you know the process name? If you do, you can build a .bat file that uses the taskkill command: microsoft.com/resources/documentation/windows/xp/all/proddocs/… (for example, taskkill /im notepad.exe).Gymkhana
hey, guys, thank you all but I cannot vote you guys up or mark correct answer here. If you guys don't mind, please answer it.Eleventh
D
13

It is rather messy but you need to do something like the following:

START "do something window" dir
FOR /F "tokens=2" %I in ('TASKLIST /NH /FI "WINDOWTITLE eq do something window"' ) DO SET PID=%I
ECHO %PID%
TASKKILL /PID %PID%

Found this on this page. (archived)

(This kind of thing is much easier if you have a UNIX / LINUX system ... or if you run Cygwin or similar on Windows.)

Drury answered 15/4, 2010 at 11:3 Comment(2)
PowerShell also makes this trivial.Baggy
If you want to kill all java.exe processes : taskkill /F /IM java.exe /T. Thus said, thanks for the FOR loop I reused elsewhere :)Lorettelorgnette
Q
68

When I ran taskkill to stop the javaw.exe process it would say it had terminated but remained running. The jqs process (java qucikstart) needs to be stopped also. Running this batch file took care of the issue.

taskkill /f /im jqs.exe
taskkill /f /im javaw.exe
taskkill /f /im java.exe
Qualls answered 17/10, 2011 at 15:39 Comment(1)
You may be better off actually looking at started services - sometimes new java processes spawn again in some sort of failover - so you'd be killing processes forever!Duchess
A
32

I like this one.

wmic process where "name like '%java%'" delete

You can actually kill a process on a remote machine the same way.

wmic /node:computername /user:adminuser /password:password process where "name like '%java%'" delete

wmic is awesome!

Aggregate answered 14/2, 2013 at 20:14 Comment(4)
works nicely, but how do I get it working in a batch file?Abdullah
@Xerus The syntax should be the same. The only difference I can think of is, you might need to double the percent signs in the where clause: where "name like '%%java%%'" to specify literal percent signs as WQL wildcards, so your batch script won't try to expand %java% as a batch variable.Aggregate
@Xerus You probably ought to post a new question and show what's going on in context.Aggregate
Worked for me, Thanks!Concepcionconcept
C
24

Open the windows cmd. First list all the java processes,

jps -m

now get the name and run below command,

for /f "tokens=1" %i in ('jps -m ^| find "Name_of_the_process"') do ( taskkill /F /PID %i )

or simply kill the process ID

taskkill /F /PID <ProcessID>

sample :)

C:\Users\tk>jps -m
15176 MessagingMain
18072 SoapUI-5.4.0.exe
15164 Jps -m
3420 org.eclipse.equinox.launcher_1.3.201.v20161025-1711.jar -os win32 -ws win32 -arch x86_64 -showsplash -launcher C:\Users\tk\eclipse\jee-neon\eclipse\eclipse.exe -name Eclipse --launcher.library C:\Users\tk\.p2\pool\plugins\org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.401.v20161122-1740\eclipse_1617.dll -startup C:\Users\tk\eclipse\jee-neon\eclipse\\plugins/org.eclipse.equinox.launcher_1.3.201.v20161025-1711.jar --launcher.appendVmargs -exitdata 4b20_d0 -product org.eclipse.epp.package.jee.product -vm C:/Program Files/Java/jre1.8.0_131/bin/javaw.exe -vmargs -Dosgi.requiredJavaVersion=1.8 -XX:+UseG1GC -XX:+UseStringDeduplication -Dosgi.requiredJavaVersion=1.8 -Xms256m -Xmx1024m -Declipse.p2.max.threads=10 -Doomph.update.url=http://download.eclipse.org/oomph/updates/milestone/latest -Doomph.redirection.index.redirection=index:/->http://git.eclipse.org/c/oomph/org.eclipse.oomph.git/plain/setups/ -jar C:\Users\tk\

and

C:\Users\tk>for /f "tokens=1" %i in ('jps -m ^| find "MessagingMain"') do ( taskkill /F /PID %i )

C:\Users\tk>(taskkill /F /PID 15176  )
SUCCESS: The process with PID 15176 has been terminated.

or

C:\Users\tk>taskkill /F /PID 15176 
SUCCESS: The process with PID 15176 has been terminated.
Cyanamide answered 22/3, 2019 at 3:31 Comment(1)
jps? this is not a windows command line commandMauceri
B
17

Normally I don't have that many Java processes open so

taskkill /im javaw.exe

or

taskkill /im java.exe

should suffice. This will kill all instances of Java, though.

Baggy answered 18/4, 2010 at 8:40 Comment(0)
D
13

It is rather messy but you need to do something like the following:

START "do something window" dir
FOR /F "tokens=2" %I in ('TASKLIST /NH /FI "WINDOWTITLE eq do something window"' ) DO SET PID=%I
ECHO %PID%
TASKKILL /PID %PID%

Found this on this page. (archived)

(This kind of thing is much easier if you have a UNIX / LINUX system ... or if you run Cygwin or similar on Windows.)

Drury answered 15/4, 2010 at 11:3 Comment(2)
PowerShell also makes this trivial.Baggy
If you want to kill all java.exe processes : taskkill /F /IM java.exe /T. Thus said, thanks for the FOR loop I reused elsewhere :)Lorettelorgnette
L
8

In case you want to kill not all java processes but specif jars running. It will work for multiple jars as well.

wmic Path win32_process Where "CommandLine Like '%YourJarName.jar%'" Call Terminate

Else taskkill /im java.exe will work to kill all java processes

Laudian answered 6/1, 2017 at 7:50 Comment(0)
S
5

The answer which suggests something like taskkill /f /im java.exe will probably work, but if you want to kill only one java process instead of all, I can suggest doing it with the help of window titles. Expample:

Start

start "MyProgram" "C:/Program Files/Java/jre1.8.0_201/bin/java.exe" -jar MyProgram.jar

Stop

taskkill /F /FI "WINDOWTITLE eq MyProgram" /T

Spica answered 15/4, 2019 at 8:57 Comment(0)
P
1
FOR /F "tokens=1,2 delims= " %%G IN ('jps -l') DO IF %%H==name.for.the.application.main.Class taskkill /F /PID %%G

name.for.the.application.main.Class - replace this to your application's main class (you can find it in second column of jps -l output)

Pacer answered 26/8, 2011 at 6:12 Comment(0)
T
0

You can do this with PowerShell:

$process = Start-Process "javaw" "-jar start.jar" -PassThru
taskkill /pid $process.Id

The taskkill command will graceful close the application.

Tranche answered 6/1, 2017 at 4:1 Comment(0)
R
0

(on Windows OS without Service) Spring Boot start/stop sample.

run.bat

@ECHO OFF
IF "%1"=="start" (
    ECHO start your app name
    start "yourappname" java -jar -Dspring.profiles.active=prod yourappname-0.0.1.jar
) ELSE IF "%1"=="stop" (
    ECHO stop your app name
    TASKKILL /FI "WINDOWTITLE eq yourappname"
) ELSE (
    ECHO please, use "run.bat start" or "run.bat stop"
)
pause

start.bat

@ECHO OFF
call run.bat start

stop.bat:

@ECHO OFF
call run.bat stop
Reste answered 18/9, 2017 at 10:24 Comment(1)
Doesn't work if you use javaw (background process)Arrowwood
F
-2

This command can run from Windows Command Prompt

Kill a Java Process with PID

netstat -ano | findstr :java
taskkill /PID <ProcessID_From_Previous_Command> /F

Froth answered 19/7, 2021 at 10:48 Comment(2)
Using taskkill has already been mentioned in many other answers, including for Windows.Fondly
@Eric Aya You are absolutely right but I was confused with most of the answers and I thought to share some very simple commands. I hope the outcome of the above command will satisfy the need.Froth

© 2022 - 2024 — McMap. All rights reserved.