将 Java 控制台应用程序作为守护程序运行(后台)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6473071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:14:45  来源:igfitidea点击:

Run Java console app as a daemon (background)

javajvmjvm-arguments

提问by aleroot

I've developed a Java console application that when start, open a console window and remain in foreground, i want to start that application in background .

我开发了一个 Java 控制台应用程序,它在启动时打开一个控制台窗口并保持在前台,我想在后台启动该应用程序。

Now i launch the application by this command line :

现在我通过这个命令行启动应用程序:

java -jar myapp.jar

Is there a way to achieve this behaviour ? It's enough change the command line parameter or i need to do some change on my code ?

有没有办法实现这种行为?更改命令行参数就足够了,或者我需要对我的代码进行一些更改?

采纳答案by Steve Brisk

The answer is operating system dependent.

答案取决于操作系统。

*nix: <your command> &
Windows: (opens a new console): start <your command>
Windows: (doesn't open a new console): start /b <your command>

回答by Amir Raminfar

If you are doing this in anything unix based then you can append & to the end which will spawn a new thread and keept it running in the background.

如果您在任何基于 unix 的系统中执行此操作,那么您可以将 & 附加到末尾,这将产生一个新线程并使其在后台运行。

java -jar myapp.jar &

回答by sarnold

If you really just want it to run in the background, java -jar myapp.jar &will do the job. That way, it'll still die when the shell closes, but you can keep using your shell.

如果你真的只是想让它在后台运行,java -jar myapp.jar &就可以完成这项工作。这样,当 shell 关闭时它仍然会死,但你可以继续使用你的 shell。

If you really want it run as a daemon, nohup java -jar myapp.jar &will do the job. That way, it'll continue to live when the shell closes.

如果您真的希望它作为守护进程运行,nohup java -jar myapp.jar &它将完成这项工作。这样,当外壳关闭时它会继续存在。

If you want this to be reliable, you can prepare an init script or upstart job definition, or run it via Vixie cron(8)@rebootspecifier to make it start at boot.

如果您希望它可靠,您可以准备一个 init 脚本或upstart 作业定义,或者通过 Vixiecron(8)@reboot说明符运行它以使其在启动时启动。

回答by Jim

Given that you're using Windows, you might consider Java Service Wrapper. I have used it on a project in the past.

鉴于您使用的是 Windows,您可能会考虑Java Service Wrapper。我过去在一个项目中使用过它。