This is a “best practices” type of question. AFAIK there are two approaches (and possibly more..) when needing to execute a task prior to JVM shutdown:
- Add a
WindowListenerto the container and override thewindowClosingevent - Add a shutdown hook (i.e.
Runtime.addShutdownHook)
Which approach is the “preferred” way when it comes to Java Desktop applications? Is there a “preferred” way?
not best practices, not preferred way, only to close current JVM instance and very simple,
1)
Top-LevelContainer#setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);2) add
WindowListener3) here is rest for how to display error message from another, new JVM instance