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 OverflowDemo2s
demo2s.com › java › apache-commons-onexit-tutorial-with-examples.html
Apache Commons OnExit tutorial with examples
The following code shows how to use OnExit from org.apache.commons.scxml.model. ... import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import no.hal.scxml.scxmlxt.AbstractState; import no.hal.scxml.scxmlxt.AbstractTransition; import no.hal.scxml.scxmlxt.AbstractTransitionEvent; import no.hal.scxml.scxmlxt.AbstractUriLiteral; import no.hal.scxml.scxmlxt.Action; import no.hal.scxml.scxmlxt.AssignmentAction; import no.hal.scxml.scxmlxt.BooleanLiteral; import no.hal.scxml.scxmlxt.Condition; import no.hal.scxml.scxmlxt.DelayLiteral; import no.hal.scxm
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
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);
}
});
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
Scaler
scaler.com › home › topics › java › exit method in java
Exit Method in Java - Scaler Topics
April 3, 2024 - This article explores how to effectively use System.exit() in Java, complemented by examples and a look into label-based control flow management, ensuring you can easily navigate through Java's program control options.
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 ...
GeeksforGeeks
geeksforgeeks.org › java › system-exit-in-java
System.exit() in Java - GeeksforGeeks
April 21, 2025 - // Java program to demonstrate working of System.exit() import java.util.*; import java.lang.*; public class Geeks { public static void main(String[] args) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; for (int i = 0; i < arr.length; i++) { if (arr[i] > 4) { System.out.println("exit..."); // Terminate JVM System.exit(0); } else System.out.println("arr["+i+"] = " + arr[i]); } System.out.println("End of Program"); } }
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 ...
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.
Java Code Geeks
examples.javacodegeeks.com › home › java development › desktop java › swing
How to Close an Application Properly - Java Code Geeks
October 5, 2016 - Shutdown hooks are get called to allow some last minute clearing before JVM actually terminates. System.exit() also accept an int status parameter where a non zero value denote abnormal execute and the result returned by java command to caller. In this java tutorial we will see example of closing both Java program and Java Swing application.
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java System.exit() Example - Java Code Geeks
February 24, 2021 - Interested to learn more?Check out our detailed example on Java System.exit method, which exits current program by terminating running Java virtual machine.
GeeksforGeeks
geeksforgeeks.org › java › java-runtime-exit-method
Java Runtime exit() Method with Examples - GeeksforGeeks
July 23, 2025 - Likewise in System.exit() this method terminates the program and exits the JVM with the specified status code. In this article we are going to explore its syntax, and how we use it in our Java code with code demonstration.