I have an interactive java program that allow user to send messages to a server, which behaves kind of like a shell, accepting user's keyboard input and perform various action.
For example
myProgram> send "Login as James" to server
My program will parse the user input and perform the action, in this case, it will send the message "Login as James" to the server.
One of the command that I support its "quit", which will close all the server connection, clean up resources and shutdown the app. and the code for handling this quit command is
private void shutdown()
{
closeAllConnection();
cleanup();
System.out.println("Thank you for using the tool, have a nice day!");
System.exit(0);
}
When I run findbug against my code, a DM_EXIT bug is raised
Bug: new myProgram.messagingTools.main(String[]) invokes System.exit(...), which shuts down the entire virtual machine
Pattern id: DM_EXIT, type: Dm, category: BAD_PRACTICE
Invoking System.exit shuts down the entire Java virtual machine. This should only been done when it is appropriate. Such calls make it hard or impossible for your code to be invoked by other code. Consider throwing a RuntimeException instead.
and it complains that System.exit should not be used to shutdown the program.
Anyone have suggestion on how should I "Shutdown the application when my program receive the 'quit' command" ?