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
🌐
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 ...
🌐
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.
🌐
Yourkit
yourkit.com › docs › java-profiler › latest › help › onexit.jsp
YourKit Java Profiler help - Callback onExit()
Fully featured low overhead profiler for Java EE and Java SE platforms. ... Easy-to-use performance and memory .NET profiler for Windows, Linux and macOS. ... Secure and easy profiling in cloud, containers and clustered environments. ... Performance monitoring and profiling of Jenkins, Bamboo, TeamCity, Gradle, Maven, Ant and JUnit. ... Callback method onExit() can be used instead of two separate callbacks onReturn() and onUncaughtException().
🌐
GeeksforGeeks
geeksforgeeks.org › java › system-exit-in-java
System.exit() in Java - GeeksforGeeks
April 21, 2025 - In Java, the System.exit() method is present in the java.lang package. This method is used to explicitly terminate the currently running Java program. This method takes a status code.
🌐
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.".
Find elsewhere
🌐
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.
🌐
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 ...
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › lang › ProcessHandleImpl.java
jdk/src/java.base/share/classes/java/lang/ProcessHandleImpl.java at master · openjdk/jdk
public CompletableFuture<ProcessHandle> onExit() { if (this.equals(current)) { throw new IllegalStateException("onExit for current process not allowed"); } · return ProcessHandleImpl.completion(pid(), false) .handleAsync((exitStatus, unusedThrowable) -> this); } ·
Author   openjdk
🌐
Processing Forum
forum.processing.org › one › topic › run-code-on-exit.html
Run code on exit - Processing Forum
October 20, 2010 - stop() is a method of Java's Applet, and as such it isn't deprecated. dispose() should " Not to be called or overriden by users" (says the JavaDoc)...
🌐
Edureka
edureka.co › blog › system-exit-in-java
Exit function in Java | System.exit() Method with Example | Edureka
November 29, 2022 - Exit function in Java exits the current program by terminating Java Virtual Machine. System.exit() method calls the exit method in class Runtime.
🌐
Apache Commons
commons.apache.org › proper › commons-scxml › javadocs › 0.8 › index.html
OnExit (Commons SCXML 0.8 API)
Commons SCXML 0.8 API · Frame Alert · This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link toNon-frame version
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › src › java.base › share › classes › java › lang › ProcessHandle.java
openjdk-jdk11/src/java.base/share/classes/java/lang/ProcessHandle.java at master · AdoptOpenJDK/openjdk-jdk11
March 2, 2019 - * {@link java.util.concurrent.CompletableFuture#complete completed} regardless · * of the exit status of the process. * The {@code onExit} method can be called multiple times to invoke · * independent actions when the process exits. * <p> * Calling {@code onExit().get()} waits for the process to terminate and returns ·
Author   AdoptOpenJDK
🌐
Baeldung
baeldung.com › home › java › core java › guide to java.lang.process api
Guide to java.lang.Process API | Baeldung
June 10, 2025 - We might want to run something on the termination of the process. This can be achieved by using the onExit() method in the java.lang.ProcessHandle interface. The method returns us a CompletableFuture, which provides the ability to trigger dependent operations when the CompletableFuture is completed.
🌐
automateNow
automatenow.io › home › how to exit a program in java
How to Exit A Program in Java | automateNow
November 1, 2024 - The System.exit(int status) method is the most direct way to terminate a Java program. This method has a status parameter to indicate the exit status.
🌐
OpenJDK
bugs.openjdk.org › issues
Process/ProcessHandle.onExit() spec need to be improved
JDK-8140213 Process/ProcessHandle.onExit() spec need to be improved · JDK-8139982 Re-examine java.management dependency on java.util.logging.LoggingMXBean · JDK-8139927 Improve documentation for CompletableFuture composition · JDK-8138963 java.lang.Objects new method to default to non-null ·