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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3170975/
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-10-30 00:40:28  来源:igfitidea点击:

How to run a java program without a console

javacommand-lineheadless

提问by Ramesh

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

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

回答by bakkal

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

在 Unix 和 GNU/Linux 系统上,您可以nohup像这样运行程序,假设它是一个 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 servicestoo, which is more manageable than bare javaprocesses.

还有一些包可以将您的 Java 程序包装到服务中,这比裸java进程更易于管理。

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

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

回答by Pascal Thivent

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

一个“替代”nohup将是screenscreen非常有用,它允许您运行任何任务、分离屏幕并让它在后台运行。您可以稍后恢复。

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.hostnameis one of the returned values from the output from screen -r.

要运行任务:

screen <command_you_want_to_run>

然后<ctrl> <a> <d>从屏幕会话中分离。

下次登录时,您可以使用以下命令重新附加到屏幕会话:

screen -r

如果您有多个屏幕会话正在运行,您将看到它们的详细信息,并且可以像这样连接到它们:

screen -r 1234.pts-1.hostname

... 其中1234.pts-1.hostname是 screen -r 输出的返回值之一。

回答by Michael B.

Use the javawcommand instead of java.

使用javaw命令而不是java.

回答by Craig Trader

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).

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