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);
}
});
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
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
Run code on exit - Processing Forum
Is there any onExit() event or something that I could use for this? More on forum.processing.org
[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
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.".
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.
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
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