保持 Java 程序在没有标准输入的情况下运行
我想要完成的是启动一个 Java 程序并让它继续运行,直到用户使用 control-C 杀死它。我意识到可以通过创建 BufferedReader 并让它在读取 BufferedReader 时无限循环来做到这一点,但我所做的涉及我后台 Java 程序(例如 java -jar app.jar &),这会杀死标准所以这个方法行不通。我读过一些有关 Java 守护线程的内容,但我也不认为在这种情况下这是正确的解决方案,因为我希望 JVM 保持活动状态。
任何帮助将不胜感激。
谢谢, 克里斯
What I would like to accomplish is to start a Java program and have it keep running until the user kills it with a control-C. I realize that it is possible to do this by creating a BufferedReader and having it endlessly loop while reading the BufferedReader, but what I am doing involves me backgrounding the Java program (e.g., java -jar app.jar &) which kills standard in so that method would not work. I've read a bit on Java's daemon threads, but I also do not think that is the correct solution in this instance because I want the JVM to stay alive.
Any help would be much appreciated.
Thanks,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你可能是从不同的进程启动java程序?一个潜在的选择可能是集成 Java Service Wrapper ?根据您所做的事情,许可可能是一个问题,您可能不会使用 ctrl-c 终止该进程,但这是一个想法(除了上面给出的其他想法之外)。
I'm guessing you're launching the java program from a different process perhaps? A potential option may be integrating the Java Service Wrapper ? Depending on what you are doing, licensing may be an issue, and you probably wouldn't kill the process with a ctrl-c, but it's a thought (in addition to the others given above).