<p>I would like to save the programs settings every time the user exits the program. So I need a way to call a function when the user quits the program. How do I do that?</p>

<p>I am using Java 1.5.</p>

[{'answer_id': 63701, 'author': 'Mat Mannion', 'author_id': 6282, 'author_profile': 'https://Stackoverflow.com/users/6282', 'pm_score': 6, 'selected': True, 'text': '<p>You can add a shutdown hook to your application by doing the following:</p>\n\n<pre><code>Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {\n    public void run() {\n        // what you want to do\n    }\n}));\n</code></pre>\n\n<p>This is basically equivalent to having a try {} finally {} block around your entire program, and basically encompasses what\'s in the finally block.</p>\n\n<p>Please note the <a href="https://stackoverflow.com/questions/63687/calling-function-when-program-exits-in-java#63886">caveats</a> though!</p>\n'}, {'answer_id': 63886, 'author': 'mfx', 'author_id': 8015, 'author_profile': 'https://Stackoverflow.com/users/8015', 'pm_score': 4, 'selected': False, 'text': '<p>Adding a shutdown hook <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)" rel="noreferrer">addShutdownHook(java.lang.Thread)</a> is probably what you look for. There are problems with that approach, though:</p>\n\n<ul>\n<li>you will lose the changes if the program aborts in an uncontrolled way (i.e. if it is killed)</li>\n<li>you will lose the changes if there are errors (permission denied, disk full, network errors)</li>\n</ul>\n\n<p>So it might be better to save settings immediately (possibly in an extra thread, to avoid waiting times).</p>\n'}, {'answer_id': 64773, 'author': 'Avrom', 'author_id': 8840, 'author_profile': 'https://Stackoverflow.com/users/8840', 'pm_score': 0, 'selected': False, 'text': '<p>Are you creating a stand alone GUI app (i.e. Swing)?</p>\n\n<p>If so, you should consider how you are providing options to your users how to exit the application.\nNamely, if there is going to be a File menu, I would expect that there will be an "Exit" menu item.\nAlso, if the user closes the last window in the app, I would also expect it to exit the application.\nIn both cases, it should call code that handles saving the user\'s preferences.</p>\n'}, {'answer_id': 69288, 'author': 'Kevin Day', 'author_id': 10973, 'author_profile': 'https://Stackoverflow.com/users/10973', 'pm_score': 0, 'selected': False, 'text': '<p>Using Runtime.getRuntime().addShutdownHook() is certainly a way to do this - but if you are writing Swing applications, I strongly recommend that you take a look at JSR 296 (Swing Application Framework)</p>\n\n<p>Here\'s a good article on the basics:  <a href="http://java.sun.com/developer/technicalArticles/javase/swingappfr/" rel="nofollow noreferrer">http://java.sun.com/developer/technicalArticles/javase/swingappfr/</a>.</p>\n\n<p>The JSR reference implementation provides the kind of features that you are looking for at a higher level of abstraction than adding shutdown hooks.</p>\n\n<p>Here is the reference implementation:  <a href="https://appframework.dev.java.net/" rel="nofollow noreferrer">https://appframework.dev.java.net/</a></p>\n'}]
['https://Stackoverflow.com/questions/63687', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']