Kill a java process from the command-line or a bat file

Today I spent a few hours to search how to kill a java process using the Windows command line tool or with a bat file easily. So to help me the next and to avoid people to lose their time I’m going to explain you how to do this.

First of all, you must already know that, when you execute a Java program he runs on a JVM (Java Virtual Machine). Thus when you execute the “tasklist” command you’ll not be able to see your Java process name. Here an example:

C:\Users\YannickL>tasklist

Image Name                     PID Session Name     Session#    Mem Usage
========================= ======== ================ ======== ============
eclipse.exe                   2656 Console                 1     4 496 Ko
javaw.exe                     5076 Console                 1   304 264 Ko
java.exe                      6092 Console                 1    42 980 Ko
cmd.exe                       3620 Console                 1     3 980 Ko
java.exe                      1072 Console                 1    50 768 Ko
tasklist.exe                  4428 Console                 1     6 764 Ko

Fortunately when you have the JDK installed you can see all the java processes which are running on your machine by using the Java Virtual Machine Process Status Tool. To use it just type the “jps” command:

C:\Users\YannickL>jps -m
1072 Main
6092 PaciferTestClient
5664 Jps -m
5076 Program Files\Eclipse\\plugins/org.eclipse.equinox.launcher_1.3.0.v20120522
-1813.jar -os win32 -ws win32 -arch x86_64 -showsplash C:\Program Files\Eclipse\
\plugins\org.eclipse.platform_4.2.0.v201206081400\splash.bmp -launcher C:\Progra
m Files\Eclipse\eclipse.exe -name Eclipse --launcher.library C:\Program Files\Ec
lipse\\plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20120522
-1813\eclipse_1503.dll -startup C:\Program Files\Eclipse\\plugins/org.eclipse.eq
uinox.launcher_1.3.0.v20120522-1813.jar --launcher.overrideVmargs -exitdata a60_
64 -product org.eclipse.epp.package.java.product -vm C:\Windows\system32\javaw.e
xe -vmargs -Dosgi.requiredJavaVersion=1.5 -Dhelp.lucene.tokenizer=standard -Xms4
0m -Xmx384m -XX:MaxPermSize=256m -jar C:\Program Files\Eclipse\\plugins/org.ecli
pse.equinox.launcher_1.3.0.v20120522-1813.jar

As you can see the “jps” command gives you all the needed information like the PID and the java process name. So by using the java process name you are now able to kill the desired process.

Here you can find the magic command line:

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

This command will execute the “jps -m” command and get the PID of the java processes which contain the given “JAVA_PROCESS_NAME” and execute a “taskkill /F /PID” over.
N.B.1: replace the “JAVA_PROCESS_NAME” by your own process name.
N.B.2: For the bat files put a double % (%%i) instead of one.

For example, to kill the Eclipse program type this command:

C:\Users\YannickL>for /f "tokens=1" %i in ('jps -m ^| find "Eclipse"') do ( taskkil
l /F /PID %i )
C:\Users\YannickL>(taskkill /F /PID 5076  )
SUCCESS: The process with PID 5076 has been terminated. 

I hope it’ll be helpful. 🙂

1 Star2 Stars3 Stars4 Stars5 Stars (38 votes, average: 4.79 out of 5)
Loading...

11 comments

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. suneeti · August 22, 2012

    Thank you. It solved my problem. I need to stop one specific java process. Using this command I created a batch file and successful destroyed that java process.

  2. Jim Ryan · August 22, 2012

    Thanks for this post. I’m running a jar file process, so your exact shutdown line didn’t work for me because the process name isn’t listed with “-m”. So, I found that “-l” will list jar file processes’ names. Using -l instead of -m and using %% I was able to make a nice shutdown.bat for my SpringBoot/Tomcat process.

  3. Jakub Mareda · August 22, 2012

    Thanks a lot! I didn’t believe I will achieve this under 2 hours and I got a solution in 3 minutes 🙂

  4. caligula-nova · August 22, 2012

    Hi there. Thanks for this.
    Problem: one needs to have JDK installed, right? Is there a way to have this just for JRE?
    I’m making a .exe based on a .bat for other users, and many have JRE, but not JDK.

  5. Charini · August 22, 2012

    Thank you very much for this!!!. None of the bat commands I attempted so far didn’t work.
    Note: % must be replaced with %% inside bat file

  6. Suresh · August 22, 2012

    This is pretty awesome and I really liked it.
    I need one small help. I need the below:
    I want a program to identify if the Java process is running, print the PID, if not print Java process isn’t running.

  7. Pingback: Cómo pasar el comando a la aplicación java usando un archivo por lotes
  8. Teerawich · August 22, 2012

    Thanks a lot. This solution can help me.

  9. Chris · August 22, 2012

    Your my hero.
    I was afraid I had another day of windows scripting before me and was already considering to jump out of the next window.
    This thread saved my life XD

  10. Raza · August 22, 2012

    Thanks for this thread, I want to kill all java processes. So what changes I have to made.

  11. Jai · August 22, 2012

    This post was created on 2012 and it is still helpful in 2022 and years to come. Thank you.