How to properly shutdown a java command line program
Asked Answered
S

4

5

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" ?

Sharolynsharon answered 19/3, 2013 at 3:27 Comment(0)
F
10

You're fine. The warning says "This should only been done when it is appropriate" (emphasis mine)

This is an appropriate way to use System.exit so you can ignore the warning.

Alternatively, if your entire program is run from main without spawning any new threads then you can just return from main and let the program shut down by itself. If you have any new threads (especially if you're using Swing) then you're probably better off just using System.exit...unless those threads also need to do some cleanup, in which case you'll need a way to shut them all down gracefully.

Frizz answered 19/3, 2013 at 3:29 Comment(1)
if you place the System.exit(0) call at the end of the main method, then findbugs does not complain about it. FindBugs is just saying that the System.exit calls should not be in various places in your code.Foreyard
C
1

System.exit() is used for abrupt exit. Although it does invoke any shutdown hooks it doesn't allow the threads without one to end properly. Invocation of this method is normally for "catastrophic error exit".

http://www.javapractices.com/topic/TopicAction.do?Id=86

Therefore, as Sudhanshu suggests proper way would be to send a signal to your interpreter loop to break if input is 'quit'. Besides this your code should track all threads and resources such that their cleanup is possible after quit.

Corry answered 19/3, 2013 at 3:39 Comment(0)
A
0

If you are accepting (waiting for) commands in a loop, just break out of the loop whenever user enters quit. That could be the condition of your while loop ... something like while(!command.equalsIgnoreCase("quit")) { } Call shutdown() after you exit the loop.

Awoke answered 19/3, 2013 at 3:29 Comment(0)
H
-2

Use Runtime.getRuntime().exit(0) instead. This worked for me. Anyway eventually System.exit(0) is going to call this.

Hildehildebrand answered 16/11, 2017 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.