简体   繁体   English

如何正确关闭java命令行程序

[英]How to properly shutdown a java command line program

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. 我有一个交互式java程序,允许用户将消息发送到服务器,服务器的行为类似于shell,接受用户的键盘输入并执行各种操作。

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. 我的程序将解析用户输入并执行操作,在这种情况下,它将向服务器发送消息“Login as James”。

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 当我对我的代码运行findbug时,会引发DM_EXIT错误

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. 并且它抱怨System.exit不应该用于关闭程序。

Anyone have suggestion on how should I "Shutdown the application when my program receive the 'quit' command" ? 有人建议我应该如何“在我的程序收到'退出'命令后关闭应用程序”?

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. 这是使用System.exit的合适方法,因此您可以忽略该警告。

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. 或者,如果您的整个程序是从main运行而不生成任何新线程,那么您可以从main返回并让程序自行关闭。 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. 如果你有任何新线程(特别是如果你使用Swing)那么你最好只使用System.exit ...除非那些线程需要做一些清理,在这种情况下你需要一个方法来把他们全部关闭。

System.exit() is used for abrupt exit. System.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 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'. 因此,正如Sudhanshu建议的那样,如果输入“退出”,正确的方法是向解释器循环发送信号以中断。 Besides this your code should track all threads and resources such that their cleanup is possible after quit. 除此之外,您的代码应该跟踪所有线程和资源,以便在退出后可以进行清理。

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. 这可能是你的while循环的条件......类似于while(!command.equalsIgnoreCase("quit")) { }退出循环后调用shutdown()

Use Runtime.getRuntime().exit(0) instead. 请改用Runtime.getRuntime().exit(0) This worked for me. 这对我有用。 Anyway eventually System.exit(0) is going to call this. 无论如何最终System.exit(0)将调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM