title: Run JAR as a Windows service
body: <p>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. </p>
  
  <p>I already have a JAR with external <code>lib</code> dir, I can start it with <code>java -jar My.jar</code> and stop with <code>Ctrl+C</code>.</p>
  
  <p>I also checked JSL, JSmooth, and procrun from Apache with no working solution.</p>
  
  <p>I would need a working solution with a good tutorial.</p>
  
  <p><strong>Update:</strong> 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:</p>
  
  <pre><code>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%
  </code></pre>
  
  <p>I presume to</p>
  
  <ul>
  <li>run this from the same directory where the jar and prunsrv.exe is</li>
  <li>the jar has its working MANIFEST.MF</li>
  <li>and you have shutdown hooks registered into JVM (for example with context.registerShutdownHook() in Spring)...</li>
  <li>not using relative paths for files outside the jar (for example log4j should be used with <em>log4j.appender.X.File=${app.home}/logs/my.log</em> or something alike)</li>
  </ul>
  
  <p>Thanks to the apache procrun team (<a href="http://commons.apache.org/proper/commons-daemon//procrun.html" rel="nofollow noreferrer">http://commons.apache.org/proper/commons-daemon//procrun.html</a>) and to marifnst (<a href="http://a089lp.wordpress.com/tag/procrun-tutorial/" rel="nofollow noreferrer">http://a089lp.wordpress.com/tag/procrun-tutorial/</a>)</p>
  
  <p><strong>Update 2</strong>: a new good tutorial with winsv: <a href="https://dzone.com/articles/spring-boot-as-a-windows-service-in-5-minutes" rel="nofollow noreferrer">https://dzone.com/articles/spring-boot-as-a-windows-service-in-5-minutes</a></p>