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
If you need to set an exit code for your application, you have to use System.exit (I think).
But when you do not need a specific code (or 0 is fine), then I would much rather have the JVM terminate "naturally", which happens when there are no more (non-daemon) threads left.
Usually, you would just reach the end of your main method.
I would be careful with System.exit because there may be other threads that still need to do something, and exiting without shutting them down properly may cause damage. For example the embedded database might still need to flush its buffers.
If you know about these threads, you can usually arrange for them to end gracefully. If you do not know what threads are running in your program, you have bigger problems...