System.exit(0) in java

后端 未结 5 1879
北恋
北恋 2021-01-02 07:41

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 sh

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-02 08:04

    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 calling Runtime.addShutdownHook(Thread), which will allow me to clean application (close threads, and so on).

提交回复
热议问题