Close all java processes

Close all java processes

Basically every java application is terminated when end of main method is reached or when all running threads run out of code. Find more information about stopping threads (here).

It is possible to force java application to terminate immediately with simple method call:

This method can be called anytime from within the code (eg. when user clicks ‘Quit’ button or in case of fatal errors). JVM is terminated immediately. As a consequence all data in application is lost — objects are gone, opened connections might hang in some weird state.

On Linux systems java application can be terminated from console window in the same way as any other process — kill the process. Before executing kill command you need to know the process id (PID) of your java application. This can be done in many ways. Example (PID is colored with red):

> ps -ef | grep java
501 346 242 0 0:00.04 ttys000 0:00.13 /usr/bin/java -jar javaShutdownHook.jar
501 348 254 0 0:00.00 ttys001 0:00.00 grep java

Process id can also be viewed by a tool jps which is part of JDK instalation (not JRE).

> jps -ml
346 javaShutdownHook.jar
349 sun.tools.jps.Jps -ml

Once you know the process id of process you want to terminate, simply execute:

Читайте также:  Основной код html страницы

But how to regularly stop an application without any ‘Quit’ button? Services for example. Usually they don’t have any GUI and the applications were meant to run forever in the background.

To stop an application in a regular manner you need to add ‘a shutdown hook’ to your java application. When kill command is executed, a special signal SIGTERM is sent to java process. The signal is intercepted by Runtime class (java.lang) which is the bond with the underlaying operating system, and consequently start the shutdown procedure.

A shutdown hook is a simple Thread object. The developer should implement the shutdown procedure within the run() method. Pass the Thread to the Runtime object by calling addShutdownHook(Thread t) method. When SIGTERM signal is received, Runtime class will start the thread.

Let’s try shutdown hook in action. This is the scenario: make a simple loop with counter which is increased every second (imagine this is a service). Then make a thread which will set the running flag to false and therefore exit the counter loop — then the application will reach end of main method in regular way. At the beginning of application set the Thread as a shutdown hook.

Remember: don’t call start() method to run the Thread — Runtime class will do it!

Test class with main method:

public static boolean running = true;

public static void main(String[] args)

System.out.println(«+++ JavaStopTest started +++»);

int i = 0;
while (running) System.out.println(«count — JavaStopTest ended —«);
>

public class MyShutdownHook extends Thread

@Override
public void run() System.out.println(«=== my shutdown hook activated»);

// stop running threads
// store data to DB
// close connection to DB
// disconnect.
// release other resources.

Build and run the code (from console) — you should see counter running. Open another console window, search for the PID of java process and kill the process (you have 100 seconds). Download already built jar (here).

Running example in two consoles:

CONSOLE 1: CONSOLE 2: # start application > java -jar javaShutdownHook.jar +++ StopTest started +++ count=0 count=1 count=2 count=3 . # get PID > jps -ml 7096 javaShutdownHook.jar # stop application > kill 7096 . count=51 count=52 === shutdown hook activated # application is stopped

Overriding shutdown hook

A shutdown hook is not executed if java process is terminated with:

Kill level 9 sends more effective termination signal (SIGKILL), which terminates the application immediately without running the shutdown hook.

The same happens when the system is going to reboot or shutdown. First a SIGTERM signal is sent to all processes. Then the system waits for applications to terminate regularly. If applications fail to terminate after some time, SIGKILL signal is sent to force quit an application.

Recently seen.

Most used

Hot stuff

By the way.

You can always use traditional HTML tags such as p, h1, h2, div, etc.

Raspberry Pi projects

Controll LED
DHT-11
Photoresistor

Источник

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. 

(38 votes, average: 4.79 out of 5)

Share on facebook

Share on google

Share on twitter

Share on linkedin

Share on reddit

Share on pinterest

11 comments

Cancel

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

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.

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.

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

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.

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

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.

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

Источник

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. 

(38 votes, average: 4.79 out of 5)

Share on facebook

Share on google

Share on twitter

Share on linkedin

Share on reddit

Share on pinterest

11 comments

Cancel

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

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.

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.

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

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.

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

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.

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

Источник

Оцените статью