I need to execute a method, (a method which creates a file), when I exit my program, how would I do this?
-
1What kind of program are you developing, a Command Line, Desktop, Web Application, what? How do you start it, and how do you close it? – Anthony Accioly Apr 28 '11 at 19:42
-
I start it by pressing the run button in Eclipse at the moment, it will be ran by an .jar file when it's done, you close it by just clicking the normal, windows close button. It's a very basic game at the moment. – Stan Apr 28 '11 at 19:45
-
So, it is a Swing application, or maybe a SWT application? – Anthony Accioly Apr 28 '11 at 19:46
-
It's a swing application, yes. – Stan Apr 28 '11 at 19:48
6 Answers
Add shutdown hook. See this javadoc.
Example:
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
System.out.println("In shutdown hook");
}
}, "Shutdown-thread"));
}

- 11,878
- 2
- 35
- 51
-
I'm not very sure how I can implent this into my code. Can I have any help? – Stan Apr 28 '11 at 19:46
-
if you look at Stans other questions I don't think this is what he needs. – Captain Giraffe Apr 28 '11 at 19:52
-
This works, it is what I wanted. I'm sorry for all the bad explanation, i'm kind of bad in it. – Stan Apr 28 '11 at 19:56
-
-
Note there are circumstances where this doesn't work. For example, on Linux if you `kill
` the process it will work, but if you `kill -9 – Black May 26 '21 at 01:17` it won't -
So does the run() function run before, during, or after I close the program? – CiY3 Jan 19 '22 at 15:56
Since you are using Swing. When you close your application (by pressing the close button), you could simply hide your frame. Run the method you would want which creates the file and then exit the Frame. This would result in a graceful exit. Should there be any errors/exceptions, you can log that into a separate file.
Here is the code
package test;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
public class TestFrame extends JFrame{
public TestFrame thisFrame;
public TestFrame(){
this.setSize(400, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
public static void main(String[] args){
TestFrame test = new TestFrame();
test.addComponentListener(new ComponentAdapter() {
@Override
public void componentHidden(ComponentEvent e) {
System.out.println("Replace sysout with your method call");
((JFrame)(e.getComponent())).dispose();
}
});
}
}
Please be aware of using shutdown hooks. As given in the Javadoc, it states that
When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook

- 34,892
- 30
- 114
- 171
-
So, I put my frame.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE ); , but, how would I actually run the method I want now? – Stan Apr 28 '11 at 19:52
-
@Stan: hi stan, i have updated with code. is there any doubt you have, ask – bragboy Apr 28 '11 at 20:00
-
you would run whatever method you want inside the componentHidden() method. – bragboy Apr 28 '11 at 20:01
-
How does the ComponentAdapter avoid the problem of a long running computation during shutdown? In both ways Windows tries to close the application and then the application does its last computation, right? So where is the difference? – user3019423 Sep 01 '21 at 20:22
Implement a WindowListener (or extend WindowAdapter), use the windowClosing (if errors in the process should prevent the window from closing or something like that) or windowClosed method.
Heres the link for the official Sun (Erm... Oracle) tutorial that tells you how to create a WindowListener and add that to your JFrame: http://download.oracle.com/javase/tutorial/uiswing/events/windowlistener.html

- 21,918
- 9
- 70
- 118
addWindowListener is better solution:
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
// run methods before closing
try {
Runtime.getRuntime().exec("taskkill /f /im java.exe");
} catch (IOException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
}
});

- 432
- 6
- 15
class ExitThread extends Thread {
public void run() {
// code to perform on exit goes here
}
}
//in main or wherever, beginning of execution
ExitThread t = new ExitThread();
//don't call t.start(), the hook will do it on exit
addShutdownHook(t);
Haven't tested it, but that should get you going. Also, you don't have to use the default constructor if you want to pass some params to that thread.

- 1,086
- 7
- 15