将JAR作为Windows服务运行 [英] Run JAR as a Windows service
问题描述
我有一个JAR文件,我想注册并将其作为Windows服务运行。使用配置良好的JAR和已经注册的JVM关闭挂钩,这不应该是一项大工作。
I have a JAR file and I would like to register and run it as a Windows service. With a well-configured JAR and already registered JVM shutdown hooks it should not be a big work to do this.
我已经有一个外部 lib
dir的JAR,我可以用 java启动它-jar My.jar
并停止 Ctrl + C
。
I already have a JAR with external lib
dir, I can start it with java -jar My.jar
and stop with Ctrl+C
.
我还检查过来自Apache的JSL,JSmooth和procrun,没有可行的解决方案。
I also checked JSL, JSmooth, and procrun from Apache with no working solution.
我需要一个有良好教程的工作解决方案。
I would need a working solution with a good tutorial.
更新:我成功同时使用procrun(最后)和手动.net服务包装器......这是我的install.bat的procrun版本的代码:
Update: I succeeded with both procrun (at last), and the manual .net service wrapper too... Here is the code for the procrun version of my 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%
我认为
- 从jar和prunsrv.exe所在的同一目录运行/ li>
- jar有它的工作MANIFEST.MF
- 并且你有注册到JVM的关闭钩子(例如在Spring中使用context.registerShutdownHook())。 ..
- 不使用jar外文件的相对路径(例如log4j应与 log4j.appender.X.File = $ {app.home} / logs /一起使用my.log 或类似的东西)
- 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.log or something alike)
感谢apache procrun团队( http://commons.apache.org/proper/commons-daemon//procrun.html )和marifnst( http://a089lp.wordpress.com/tag/procrun-教程/ )
Thanks to the apache procrun team (http://commons.apache.org/proper/commons-daemon//procrun.html) and to marifnst (http://a089lp.wordpress.com/tag/procrun-tutorial/)
更新2 :一个带有winsv的新的好教程: https://dzone.com/articles/spring-boot-as-a-windows-service -in-5分钟
Update 2: a new good tutorial with winsv: https://dzone.com/articles/spring-boot-as-a-windows-service-in-5-minutes
推荐答案
从这些样本中编写自己的服务:
Write your own service from these samples:
- http: //www.codeproject.com/Articles/3990/Simple-Windows-Service-Sample
- http://msdn.microsoft.com/en-us/library/windows/desktop/bb540476%28v=vs.85%29.aspx?bcsi-ac-bbaf765720ef3335=20190C4200000503o7MxKbMq7UL6Af4O24rqKSGuh1gPAwAAAwUAAPvrLQAIBwAAbwEAAGhCCQA =
- http://code.msdn.microsoft.com/windowsdesktop/CppWindowsService-cacf4948?bcsi-ac-bbaf765720ef3335=20190C4200000503o7MxKbMq7UL6Af4O24rqKSGuh1gPAwAAAwUAAPvrLQAIBwAAbwEAAGhCCQA=
- http://www.codeproject.com/Articles/3990/Simple-Windows-Service-Sample
- http://msdn.microsoft.com/en-us/library/windows/desktop/bb540476%28v=vs.85%29.aspx?bcsi-ac-bbaf765720ef3335=20190C4200000503o7MxKbMq7UL6Af4O24rqKSGuh1gPAwAAAwUAAPvrLQAIBwAAbwEAAGhCCQA=
- http://code.msdn.microsoft.com/windowsdesktop/CppWindowsService-cacf4948?bcsi-ac-bbaf765720ef3335=20190C4200000503o7MxKbMq7UL6Af4O24rqKSGuh1gPAwAAAwUAAPvrLQAIBwAAbwEAAGhCCQA=
进入 onStart
你必须做 CreateProcess(java, - jar,MyJar.jar)
,保持其PID
Into onStart
you have to do CreateProcess( "java", "-jar", "MyJar.jar" )
, keep its PID
进入 onStop
你必须通过PID杀死
Into onStop
you have to kill by the PID
这篇关于将JAR作为Windows服务运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!