将 Java 应用程序作为服务运行

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

Run Java application as a service

javawindowswindows-services

提问by JasCav

I would like to run a Java application as a service. Unfortunately, I am limited in that I can't use something like the Java Service Wrapper(which does appear to be an excellent tool).

我想将 Java 应用程序作为服务运行。不幸的是,我的局限性在于我不能使用像Java Service Wrapper这样的东西(它似乎是一个很好的工具)。

Is there any way of running an executable JAR, as a service, without relying on external applications? I currently have the service installed, but it fails to start. This is where I am getting stuck and I haven't been able to find anything on Google other than information about the JSW.

有什么方法可以在不依赖外部应用程序的情况下将可执行 JAR 作为服务运行吗?我目前已安装该服务,但无法启动。这就是我陷入困境的地方,除了有关 JSW 的信息之外,我在 Google 上找不到任何内容。

采纳答案by ykaganovich

There's an LGPL clone of the Java Service Wrapper: http://yajsw.sourceforge.net

有一个 Java Service Wrapper 的 LGPL 克隆:http: //yajsw.sourceforge.net

BTW, IANAL, but I suspect that JSW people are spreading FUD, and their software can be used to service-enable commercial applications under GPL license, just like one can gzip a commercial app for redistribution. I could be completely wrong about this, though :)

顺便说一句,IANAL,但我怀疑 JSW 人正在传播 FUD,他们的软件可用于在 GPL 许可下为商业应用程序提供服务,就像可以 gzip 商业应用程序进行重新分发一样。不过,我可能完全错了:)

回答by dustmachine

I haven't tried it (yet), but Launch4jlooks like it could suit your needs.

我还没有尝试过(还),但Launch4j看起来可以满足您的需求。

回答by Wolfgang

A program that should run as windows service must provide certain functions that the windows service manager uses to communicate with that service.

应该作为 Windows 服务运行的程序必须提供 Windows 服务管理器用来与该服务通信的某些功能。

As long as there is no JVM that implements this functions directly (and I know of none) you will need some kind of wrapper.

只要没有 JVM 直接实现此功能(我知道没有),您将需要某种包装器。

I have successfully used srvanyfor a java based windows service (Basically it allows to run any program as windows service and it works fine with java)

我已经成功地将srvany用于基于 Java 的 Windows 服务(基本上它允许将任何程序作为 Windows 服务运行,并且它可以与 Java 一起正常工作)

回答by Peter Smith

one more option winrun4j. the license is eclipse's CPL.

还有一个选项winrun4j。许可证是eclipse的CPL。

回答by cpierceworld

Another option, Apache Commons Daemon's procrun.

另一种选择,Apache Commons Daemon 的 procrun。

See http://commons.apache.org/daemon/

http://commons.apache.org/daemon/

回答by toba303

The most simple way I found was RunAsService.

我发现的最简单的方法是RunAsService

A co-worker recommended a tool called SC, but I did not try it.

同事推荐了一个叫SC的工具,但是我没有尝试。

回答by Thorbj?rn Ravn Andersen

JSmooth can do it, and it is scriptable with ant.

JSmooth 可以做到,并且可以用 ant 编写脚本。

回答by Anthony O.

You can use NSSMlike this:

您可以像这样使用NSSM

nssm install MyService "%JAVA_HOME%\bin\java.exe" -jar "path\to\the\file.jar"