Runtime.getRuntime().addShutdownHook(new Thread()
{
    @Override
    public void run()
    {
        updateZonas();
        db.close();
    }
});

This works for any Java application(Swing/AWT/Console)

Answer from Santhosh Kumar Tekuri on Stack Overflow
🌐
Demo2s
demo2s.com › java › apache-commons-onexit-tutorial-with-examples.html
Apache Commons OnExit tutorial with examples
The following code shows how to use OnExit from org.apache.commons.scxml.model. ... import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import no.hal.scxml.scxmlxt.AbstractState; import no.hal.scxml.scxmlxt.AbstractTransition; import no.hal.scxml.scxmlxt.AbstractTransitionEvent; import no.hal.scxml.scxmlxt.AbstractUriLiteral; import no.hal.scxml.scxmlxt.Action; import no.hal.scxml.scxmlxt.AssignmentAction; import no.hal.scxml.scxmlxt.BooleanLiteral; import no.hal.scxml.scxmlxt.Condition; import no.hal.scxml.scxmlxt.DelayLiteral; import no.hal.scxm
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › core › managing-processes-asynchronously-onexit-method.html
Handling Processes When They Terminate with the onExit Method
October 20, 2025 - Afterward, it calls onExit().thenAccept(onExitMethod) on each of the processes; onExitMethod prints the process ID (PID), exit status, and output of the process. public class ProcessTest { // ... static public void startProcessesTest() throws IOException, InterruptedException { List<ProcessBuilder> greps = new ArrayList<>(); greps.add(new ProcessBuilder("/bin/sh", "-c", "grep -c \"java\" *")); greps.add(new ProcessBuilder("/bin/sh", "-c", "grep -c \"Process\" *")); greps.add(new ProcessBuilder("/bin/sh", "-c", "grep -c \"onExit\" *")); ProcessTest.startSeveralProcesses (greps, ProcessTest::pri
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
Scaler
scaler.com › home › topics › java › exit method in java
Exit Method in Java - Scaler Topics
April 3, 2024 - This article explores how to effectively use System.exit() in Java, complemented by examples and a look into label-based control flow management, ensuring you can easily navigate through Java's program control options.
🌐
Edureka
edureka.co › blog › system-exit-in-java
Exit function in Java | System.exit() Method with Example | Edureka
November 29, 2022 - If these questions bother you, you have landed at the right place. What can you do is, simply use a System.exit() Method which terminates the current Java Virtual Machine running on the system.
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › lang › Process.html
Process (Java SE 9 & JDK 9 )
Using onExit is an alternative to waitFor that enables both additional concurrency and convenient access to the result of the Process. Lambda expressions can be used to evaluate the result of the Process execution. If there is other processing to be done before the value is used then onExit ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › system-exit-in-java
System.exit() in Java - GeeksforGeeks
April 21, 2025 - // Java program to demonstrate working of System.exit() import java.util.*; import java.lang.*; public class Geeks { public static void main(String[] args) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; for (int i = 0; i < arr.length; i++) { if (arr[i] > 4) { System.out.println("exit..."); // Terminate JVM System.exit(0); } else System.out.println("arr["+i+"] = " + arr[i]); } System.out.println("End of Program"); } }
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › lang › Process.html
Process (Java SE 10 & JDK 10 )
Using onExit is an alternative to waitFor that enables both additional concurrency and convenient access to the result of the Process. Lambda expressions can be used to evaluate the result of the Process execution. If there is other processing to be done before the value is used then onExit ...
🌐
automateNow
automatenow.io › home › how to exit a program in java
How to Exit A Program in Java | automateNow
November 1, 2024 - Learn how to exit a Java program using System.exit(), returning from the main method, returning to the main method, or throwing an exception with detailed examples.
🌐
ExamClouds
examclouds.com › home › java core
Java System.exit() Method — How to Terminate a Program
Learn how to use the System.exit() method in Java to stop program execution. Understand its syntax, exit codes, and real-life usage examples.
🌐
LabEx
labex.io › tutorials › java-how-to-use-system-exit-method-419208
How to use system exit method | LabEx
Learn essential Java system exit techniques for graceful application termination, error handling, and resource management with practical code examples and best practices.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › desktop java › swing
How to Close an Application Properly - Java Code Geeks
October 5, 2016 - Shutdown hooks are get called to allow some last minute clearing before JVM actually terminates. System.exit() also accept an int status parameter where a non zero value denote abnormal execute and the result returned by java command to caller. In this java tutorial we will see example of closing both Java program and Java Swing application.
🌐
CodeGym
codegym.cc › java blog › core java › system.exit() in java
System.exit() in Java
September 28, 2023 - The System class in Java contains fields and methods to handle the SystemOne of them is the System.exit () method used in the Java language when you need to terminate a program or rather JVM instance that is currently running.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java System.exit() Example - Java Code Geeks
February 24, 2021 - Interested to learn more?Check out our detailed example on Java System.exit method, which exits current program by terminating running Java virtual machine.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-runtime-exit-method
Java Runtime exit() Method with Examples - GeeksforGeeks
July 23, 2025 - Likewise in System.exit() this method terminates the program and exits the JVM with the specified status code. In this article we are going to explore its syntax, and how we use it in our Java code with code demonstration.
🌐
Quora
quora.com › How-do-I-exit-a-Java-program-without-System-exit
How to exit a Java program without System.exit - Quora
Answer (1 of 7): I don't really understand the question. The program terminates when it's done! So just stop having the program do stuff.