如何从java jar创建windows服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1617458/
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
How to create windows service from java jar?
提问by Rakesh Juyal
I have an executable JAR file. Is it possible to create a Windows service of that JAR? Actually, I just want to run that on startup, but I don't want to place that JAR file in my startup folder, neither in the registry.
我有一个可执行的 JAR 文件。是否可以创建该 JAR 的 Windows 服务?实际上,我只想在启动时运行它,但我不想将该 JAR 文件放在我的启动文件夹中,也不想放在注册表中。
回答by jqno
We use the Tanuki Service Wrapperfor that. It's very configurable.
为此,我们使用Tanuki Service Wrapper。这是非常可配置的。
回答by Persimmonium
Tanuki changed license of jsw some time ago, if I was to begin a project, I would use Yet Another Java Service Wrapper, http://yajsw.sourceforge.net/that is more or less an open source implementation that mimics JWS, and then builds on it and improves it even further.
狸改变JSW的许可前一段时间,如果我是开始的一个项目,我会用然而,另一个Java服务包装,http://yajsw.sourceforge.net/是或多或少的开源实现,模仿JWS,并然后以它为基础并进一步改进它。
EDIT: I have been using YAJSW for several years on several platorms (Windows, several linuxes...) and it is great, development is ongoing.
编辑:我已经在几个平台(Windows、几个 linux ......)上使用 YAJSW 好几年了,它很棒,开发正在进行中。
回答by mcdon
I've been experimenting with Apache Commons Daemon. It's supports windows (Procrun) and unix (Jsvc). Advanced Installer has a Java Service tutorialwith an example project to download. If you get their javaservice.jar running as a windows service you can test it by using "telnet 4444". I used their example because my focus was on getting a java windows service running, not writing java.
我一直在试验Apache Commons Daemon。它支持 windows ( Procrun) 和 unix ( Jsvc)。Advanced Installer 有一个Java 服务教程,其中包含一个可供下载的示例项目。如果你让他们的 javaservice.jar 作为 Windows 服务运行,你可以使用“telnet 4444”来测试它。我使用他们的例子是因为我的重点是让 java windows 服务运行,而不是编写 java.lang.
回答by BTakacs
With procrun you need to copy prunsrv to the application directory (download), and create an install.bat like this:
使用 procrun,您需要将 prunsrv 复制到应用程序目录(下载),并创建一个 install.bat,如下所示:
set PR_PATH=%CD%
SET PR_SERVICE_NAME=MyService
SET PR_JAR=MyService.jar
SET START_CLASS=org.my.Main
SET START_METHOD=main
SET STOP_CLASS=java.lang.System
SET STOP_METHOD=exit
rem ; separated values
SET STOP_PARAMS=0
rem ; separated values
SET JVM_OPTIONS=-Dapp.home=%PR_PATH%
prunsrv.exe //IS//%PR_SERVICE_NAME% --Install="%PR_PATH%\prunsrv.exe" --Jvm=auto --Startup=auto --StartMode=jvm --StartClass=%START_CLASS% --StartMethod=%START_METHOD% --StopMode=jvm --StopClass=%STOP_CLASS% --StopMethod=%STOP_METHOD% ++StopParams=%STOP_PARAMS% --Classpath="%PR_PATH%\%PR_JAR%" --DisplayName="%PR_SERVICE_NAME%" ++JvmOptions=%JVM_OPTIONS%
I presume to
我认为
- run this from the same directory where the jar and prunsrv.exe is
- the jar has its working MANIFEST.MF
- and you have shutdown hooks registered into JVM (for example with context.registerShutdownHook() in Spring)...
- not using relative paths for files outside the jar (for example log4j should be used with log4j.appender.X.File=${app.home}/logs/my.logor something alike)
- 从 jar 和 prunsrv.exe 所在的同一目录运行它
- 罐子有它的工作 MANIFEST.MF
- 并且您已将关闭挂钩注册到 JVM(例如在 Spring 中使用 context.registerShutdownHook())...
- 不使用 jar 外部文件的相对路径(例如 log4j 应该与log4j.appender.X.File=${app.home}/logs/my.log或类似的东西一起使用)
Check the procrun manualand this tutorialfor more information.
回答by kopernik
The easiest solution I found for this so far is the Non-Sucking Service Manager
到目前为止,我找到的最简单的解决方案是Non-Sucking Service Manager
Usage would be
用法是
nssm install <servicename> "C:\Program Files\Java\jre7\java.exe" "-jar <path-to-jar-file>"
回答by gcerkez
Use nssm.exe but remember to set the AppDirectory or any required libraries or resources will not be accessible. By default nssm set the current working directory to the that of the application, java.exe, not the jar. So do this to create a batch script:
使用 nssm.exe 但记住设置 AppDirectory 或任何必需的库或资源将无法访问。默认情况下,nssm 将当前工作目录设置为应用程序的工作目录,java.exe,而不是 jar。所以这样做来创建一个批处理脚本:
pushd <path-to-jar>
nssm.exe install "<service-name>" "<path-to-java.exe>" "-jar <name-of-jar>"
nssm.exe set "<service-name>" AppDirectory "<path-to-jar>"
This should fix the service paused issue.
这应该可以解决服务暂停问题。
回答by manyways
Another option is winsw: https://github.com/kohsuke/winsw/
另一种选择是 winsw:https: //github.com/kohsuke/winsw/
Configure an xml file to specify the service name, what to execute, any arguments etc. And use the exe to install. Example xml: https://github.com/kohsuke/winsw/tree/master/examples
配置一个 xml 文件来指定服务名称、要执行的内容、任何参数等。并使用 exe 进行安装。示例 xml:https: //github.com/kohsuke/winsw/tree/master/examples
I prefer this to nssm, because it is one lightweight exe; and the config xml is easy to share/commit to source code.
与 nssm 相比,我更喜欢它,因为它是一个轻量级的 exe;并且配置 xml 很容易共享/提交到源代码。
PS the service is installed by running your-service.exe install
PS通过运行安装服务 your-service.exe install
回答by hackerbuddy
You can use Windows Service Wrapper(WinSW)for this.
为此,您可以使用Windows Service Wrapper(WinSW)。
Download WinSWfrom here.
从这里下载WinSW。
Note that you have to consider the .Net version installed in your PC.
请注意,您必须考虑 PC 中安装的 .Net 版本。
Then you have to download WinSW configurations XML. You can download a sample configuration XML from the given link.
然后您必须下载 WinSW 配置 XML。您可以从给定的链接下载示例配置 XML。
Then you have to rename your WinSW binary and Config XML with same name.
然后,您必须使用相同的名称重命名 WinSW 二进制文件和配置 XML。
Ex - myservice.exe myservice.xml
例如 - myservice.exe myservice.xml
Then keep both files side by side in a same folder. Then place your Jar file in the same folder.
然后将两个文件并排保存在同一个文件夹中。然后将您的 Jar 文件放在同一个文件夹中。
Now all set. You just have change your Config XML and install the service and run.
现在一切就绪。您只需更改配置 XML 并安装服务并运行。
Hereis a complete guide to config your configuration XML in order to run Jar file and run the service as a startup service.
这是配置配置 XML 以运行 Jar 文件并将服务作为启动服务运行的完整指南。
You can install service using this command.
您可以使用此命令安装服务。
myservice.exe install
myservice.exe install
Hope this helps..!
希望这可以帮助..!