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
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › java › system-exit-in-java
System.exit() in Java - GeeksforGeeks
April 21, 2025 - Example: Using the System.exit() method to exit the currently running program. ... // Java program to demonstrate working of System.exit() import java.util.*; import java.lang.*; public class Geeks { public static void main(String[] args) { ...
🌐
Scaler
scaler.com › home › topics › java › exit method in java
Exit Method in Java - Scaler Topics
April 3, 2024 - Let's see practical implementations of System.exit(0) method in Java.
🌐
Baeldung
baeldung.com › home › java › core java › a guide to system.exit()
A Guide to System.exit() | Baeldung
January 16, 2024 - Also, it’s worth noting when we invoke a Java program from the command-line that the status code is taken into account. In the below example, when we try to execute SystemExitExample.class, if it exits the JVM by calling the System.exit with a non-zero status code, then the following echo does not get printed.
🌐
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.
🌐
Edureka
edureka.co › blog › system-exit-in-java
Exit function in Java | System.exit() Method with Example | Edureka
November 29, 2022 - What can you do is, simply use a System.exit() Method which terminates the current Java Virtual Machine running on the system.
🌐
automateNow
automatenow.io › home › how to exit a program in java
How to Exit A Program in Java | automateNow
November 1, 2024 - This method has a status parameter to indicate the exit status. By convention, a status of 0 indicates that a Java program has terminated normally, while any nonzero status code, such as 1 or -1 indicates an abnormal termination. The following example demonstrates how to use System.exit():
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › core java › system.exit() in java
System.exit() in Java
September 28, 2023 - In this example, we create a loop that generates random numbers between 0 and 10. If the generated number is 2,3, or 7, the Application needs to be terminated, and it should print which number causes the termination.
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8176272
[JDK-8176272] (process) ProcessHandle::onExit fails to wait ...
My goal was to wait on another process (Spotify) to terminate before printing something to the console: ProcessHandle handle = ProcessHandle.allProcesses() .filter(h -> h.info().commandLine().map(cmd -> cmd.contains("Spotify")).orElse(false)) .findFirst() .orElseThrow(() -> new IllegalArgumentException("No matching handle found")); System.out.println(handle.info()); ProcessHandle completed = handle.onExit().get(); System.out.println(completed.isAlive()); However this code doesn't wait on `handle` to be terminated, as the JavaDoc of onExit led me to believe: "Calling onExit().get() waits for the process to terminate and returns the ProcessHandle.".
🌐
Yourkit
yourkit.com › docs › java-profiler › latest › help › onexit.jsp
YourKit Java Profiler help - Callback onExit()
Profiler Java API · Profiler HTTP ... onUncaughtException(). It helps to avoid code duplication when, for example, a lasting event created in onEnter() should be closed on normal return as well as on exception....
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › desktop java › swing
How to Close an Application Properly - Java Code Geeks
October 5, 2016 - First option which is default is EXIT_ON_CLOSE which terminates Java swing GUI program when you click close button on JFrame window. Another option is DISPOSE_ON_CLOSE which terminates JVM if last displayable window is disposed off.
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › lang › Process.html
Process (Java SE 9 & JDK 9 )
For example, to delegate to the underlying process, it can do the following: public CompletableFuture<Process> onExit() { return delegate.onExit().thenApply(p -> this); } ... Returns a ProcessHandle for the Process. Process objects returned by ProcessBuilder.start() and Runtime.exec(java.l...
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › lang › Process.html
Process (Java SE 10 & JDK 10 )
For example, to delegate to the underlying process, it can do the following: public CompletableFuture<Process> onExit() { return delegate.onExit().thenApply(p -> this); } ... Returns a ProcessHandle for the Process. Process objects returned by ProcessBuilder.start() and Runtime.exec(java.l...
🌐
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.
🌐
Tanuki Software
wrapper.tanukisoftware.com › doc › english › prop-on-exit-n.html
wrapper.on_exit.<n> Property - Java Service Wrapper
Some applications are good about returning "exit codes" with meaning. In such cases, it may be desirable to restart the application in the event that it exits with certain exit codes. This set of properties makes this possible · It's possible to specify an action to take for any exit code ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-runtime-exit-method
Java Runtime exit() Method with Examples - GeeksforGeeks
July 23, 2025 - We can use it to terminate the Java Virtual Machine completely without completing any pending task. We can use it with a status code as 0 or any non-zero number. ... Any nonzero number shows that it's an abnormal termination of code, this means an error has occurred. We have seen both implementations of the status code with clear examples.