如何在没有控制台的情况下运行java程序

发布于 2024-09-07 21:50:45 字数 42 浏览 4 评论 0原文

即使终端关闭,我也需要运行java程序...... 在服务器中....

I need to run an java program even the terminal is closed....
in server....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情废物 2024-09-14 21:50:45

在 Unix 和 GNU/Linux 系统上,您可以像这样使用 nohup 运行程序,假设它是一个 jar:

nohup java -jar program.jar &

要将程序的输出运行到文本文件中,这样稍后你可以查看它,你可以这样做:

nohup java -jar program.jar > program.log &

有些包也可以将你的Java程序包装成服务,这比裸露的java进程更易于管理。

您可能还想使用“进程包装器”Launch4J 也许?)给你的进程起一个有意义的名字,否则你所有的Java程序都会在进程列表中显示为java,这不是很有指示性。

On Unix and GNU/Linux systems you can run the program using nohup like this, assuming it is a jar:

nohup java -jar program.jar &

To get the output of the program run into a text file so later on you can view it, you can do:

nohup java -jar program.jar > program.log &

There are packages that will wrap your Java programs into services too, which is more manageable than bare java processes.

You probably also want to use a "process wrapper" (Launch4J maybe?) to give your process a meaningful name, otherwise all your Java programs will appear as java in the process list, which isn't very indicative.

梦罢 2024-09-14 21:50:45

An "alternative" to nohup would be screen. screen is very useful and allows you to run any task, detach the screen, and let it run in the background. You can resume it later.

To run a task:

screen <command_you_want_to_run>

Then <ctrl> <a> <d> to detach from the
screen session.

The next time you log in you can
reattach to the screen session with:

screen -r

If you have multiple screen sessions
running you will be presented with
their details and can connect to them
like this:

screen -r 1234.pts-1.hostname

... where 1234.pts-1.hostname is one
of the returned values from the output
from screen -r.

榕城若虚 2024-09-14 21:50:45

使用 javaw 命令而不是 java.

Use the javaw command instead of java.

〃安静 2024-09-14 21:50:45

您想要使用无头模式。这将导致任何尝试与屏幕、键盘、鼠标等通信的调用失败,但也意味着您不需要 X 服务器(在 Unix 上)或访问控制台(在 Windows 上)。

You want to use headless mode. This will cause any calls that attempt to communicate with a screen, keyboard, mouse, etc to fail, but also means that you won't need an X server (on Unix) or access to the console (on Windows).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文