I am writing an application program using swing. I need to exit from the application by clicking the JButton for that can i use System.exit() or should i use some other methods, which is the best practice. If calling System.exit() is not best practice then tell the reason and tell the alternative way to exit from the application.
I am writing an application program using swing. I need to exit from the
Share
Personnaly, I do consider as best practice to let application exit by itself : if you write a main class with a
main(String[] args), with empty code, running it will exit silently from Java program.But, in most times, like most of developpers, I rely upon
System.exit(/an exit code/), especially for Swing applications, where the Swing EDT will run endlessly, as justkt wrote.. The known drawxback of this method is that most of applciation code is not called, what I avoid by setting a shutdown hook by callingRuntime.addShutdownHook(Thread), which will allow me to clean application (close threads, and so on).