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 OverflowOracle
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().
Top answer 1 of 3
35
Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run()
{
updateZonas();
db.close();
}
});
This works for any Java application(Swing/AWT/Console)
2 of 3
29
Are you using a JFrame? if so you can try this:
myframe.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent winEvt) {
updateZonas();
db.close();
System.exit(0);
}
});
Videos
05:52
Java Programming Skills Using Replit: System.exit() - YouTube
07:27
exit( ) method in Java - YouTube
04:50
# 69 system.exit(0) vs system.exit(1) vs system.exit(-1) java ...
08:46
63. JOptionPane: Terminate your program completely with System.exit() ...
01:51
Java Runtime Class: How to Exit JVM Using Exit Method - YouTube
01:12
How to Use System.exit() Method in Java | System class | Java ...
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().
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.".
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)...
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
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.
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
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.
Bug Database
bugs.java.com › bugdatabase › view_bug.do
Java Issue Tracker - Home
Javabugs bug viewer application
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 ·