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 - The Process.onExit and ProcessHandle.onExit methods return a CompletableFuture instance, which you can use to schedule tasks when a process terminates. Alternatively, if you want your application to wait for a process to terminate, then you can call onExit().get().
Discussions

java - ProcessHandle onExit has empty data - Stack Overflow
I'm currently testing Process API from java 9 and I have some problem with following code: Process process = new ProcessBuilder( List.of("ping", "-i", "1", "-c&qu... More on stackoverflow.com
🌐 stackoverflow.com
Running code on program exit in Java - Stack Overflow
Is it possible to write a method that System.exit will call when you terminate a program? More on stackoverflow.com
🌐 stackoverflow.com
Run code on exit - Processing Forum
Is there any onExit() event or something that I could use for this? More on forum.processing.org
🌐 forum.processing.org
October 20, 2010
[renderer1] [error] spawn java ENOENT: Error: spawn java ENOENT
Mac Monterey 12.0.1 VS Code 1.63.0 PlantUML v2.16.1 [2021-12-11 17:52:42.110] [renderer1] [error] spawn java ENOENT: Error: spawn java ENOENT at Process.ChildProcess._handle.onexit (internal/child_... More on github.com
🌐 github.com
4
December 12, 2021
🌐
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.
Find elsewhere
🌐
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.".
🌐
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.
🌐
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
🌐
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 ...
🌐
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)...
🌐
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.
🌐
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
🌐
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.
🌐
University of San Francisco Computer Science
cs.usfca.edu › ~cs272 › javadoc › api › java.base › java › lang › ProcessHandle.html
ProcessHandle (Java SE 17 & JDK 17)
Calling onExit().get() waits for the process to terminate and returns the ProcessHandle. The future can be used to check if the process is done or to wait for it to terminate.
🌐
GitHub
github.com › qjebbs › vscode-plantuml › issues › 460
[renderer1] [error] spawn java ENOENT: Error: spawn java ENOENT · Issue #460 · qjebbs/vscode-plantuml
December 12, 2021 - Mac Monterey 12.0.1 VS Code 1.63.0 PlantUML v2.16.1 [2021-12-11 17:52:42.110] [renderer1] [error] spawn java ENOENT: Error: spawn java ENOENT at Process.ChildProcess._handle.onexit (internal/child_...
Author   qjebbs