Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JAR as a Windows service [duplicate]

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.

I already have a JAR with external lib dir, I can start it with java -jar My.jar and stop with Ctrl+C.

I also checked JSL, JSmooth, and procrun from Apache with no working solution.

I would need a working solution with a good tutorial.

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%

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.log or something alike)

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

Update 2: a new good tutorial with winsv: https://dzone.com/articles/spring-boot-as-a-windows-service-in-5-minutes

like image 417
BTakacs Avatar asked Feb 27 '13 14:02

BTakacs


People also ask

How do I run a program as a Windows service?

PathToExecutable: Type the full path of the application that you want to run as a Windows service. For example: To install the "Notepad.exe" application as a Windows service with the name "Notepad", give the following command: RunAsService install "Notepad" "C:\Windows\System32\notepad.exe"


1 Answers

Try this java launcher

http://winrun4j.sourceforge.net/

free and open source

start service example at the end of the page

like image 190
Koroed Avatar answered Sep 21 '22 15:09

Koroed