Running a method when closing the program?

后端 未结 6 1142
無奈伤痛
無奈伤痛 2020-12-03 14:22

I need to execute a method, (a method which creates a file), when I exit my program, how would I do this?

相关标签:
6条回答
  • 2020-12-03 14:53

    You could also add a window on close listener to your application.

    0 讨论(0)
  • 2020-12-03 14:54

    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

    0 讨论(0)
  • 2020-12-03 14:58

    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

    0 讨论(0)
  • 2020-12-03 15:01
    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.

    0 讨论(0)
  • 2020-12-03 15:02

    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"));
    }
    
    0 讨论(0)
  • 2020-12-03 15:04

    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();
                                }
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题