62

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.

MC Emperor
  • 17,266
  • 13
  • 70
  • 106
Rakesh Juyal
  • 33,043
  • 66
  • 165
  • 212
  • 1
    Not in the registry also? Then why would you want it as a Service? – o.k.w Oct 24 '09 at 09:17
  • @o.k.w is right if you are creating windows service, It'll create entry in registry. – Ravi Parekh Sep 10 '14 at 11:28
  • I think this user meant "run as a service, not from the startup registry keys as a non service" – Warren P Sep 14 '16 at 13:54
  • 2
    You can directly create an exe or msi file from java using javapackager tool with java 8 onwards. Refer: https://stackoverflow.com/questions/68113/how-to-create-a-windows-service-from-java-app/45851799#45851799 – Steephen Aug 24 '17 at 02:00

7 Answers7

54

The easiest solution I found for this so far is the Non-Sucking Service Manager

Usage would be

nssm install <servicename> "C:\Program Files\Java\jre7\java.exe" "-jar <path-to-jar-file>"
kopernik
  • 814
  • 1
  • 7
  • 9
  • 8
    I did same and it installed successfully but as soon as I run it, it says `unexpected status SERVICE-PAUSED in response to START control` – aneela Oct 23 '14 at 05:22
  • This same problem as you mentioned. Any idea what is wrong? – krzysiek.ste May 26 '15 at 08:52
  • Same problem as you guys mentioned..any updates on the solution? – EvilInside Jul 16 '15 at 21:46
  • You can run `nssm install ` and then manage all options (there is a lot) interactively. – Destroyica Jan 20 '16 at 21:55
  • If you don't want to run as Administrator, see gcerkez's answer below. – martin jakubik Nov 29 '16 at 16:48
  • 2
    Late to the party, but with regards to the error `Unexpected status SERVICE_PAUSED in response to START control`. This happened to me because the java app was a simple "hello world" app which literally wrote to the console and existed. I think starting the service was too fast for it to fully register, so adding in more code which kept the service running longer fixed this issue. – Tim B James Jul 11 '18 at 14:11
21

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:

    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.

gcerkez
  • 285
  • 2
  • 10
  • 1
    This is good... You should add it as a comment to kopernik's answer. – martin jakubik Nov 29 '16 at 16:47
  • 2
    Setting the AppDirectory after creation of the service failed in my case. But you can launch a gui with `nssm install ` and set the AppDirectory as well as other useful parameters such as log files there and create the service in one go – JCvanDamme Jun 22 '17 at 16:22
10

I've been experimenting with Apache Commons Daemon. It's supports windows (Procrun) and unix (Jsvc). Advanced Installer has a Java Service tutorial with 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.

mcdon
  • 4,648
  • 3
  • 34
  • 36
  • For a Windows service [WinRun4j](http://winrun4j.sourceforge.net/) is also a good candidate. It can double as a Java Launcher, or Service Wrapper. – mcdon Nov 07 '14 at 14:57
  • Be warned: the documentation is awful for Apache Commons Daemon – Wisteso Aug 15 '19 at 00:07
7

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.

EDIT: I have been using YAJSW for several years on several platorms (Windows, several linuxes...) and it is great, development is ongoing.

Persimmonium
  • 15,150
  • 11
  • 43
  • 76
4

With procrun you need to copy prunsrv to the application directory (download), and create an install.bat like this:

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)

Check the procrun manual and this tutorial for more information.

BTakacs
  • 1,897
  • 3
  • 20
  • 25
2

Another option is 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

I prefer this to nssm, because it is one lightweight exe; and the config xml is easy to share/commit to source code.

PS the service is installed by running your-service.exe install

manyways
  • 3,216
  • 2
  • 18
  • 16
0

[2020 Update]

Actually, after spending some times trying the different option provided here which are quite old, I found that the easiest way to do it was to use a small paid tool built for that purpose : FireDaemon Pro. I was trying to run Selenium standalone server as a service and none of the free option worked instantly.

The tool is quite cheap (50 USD one-time-licence, 30 days trial) and it took me 5 minutes to set up the server service instead of a half a day of reading/troubleshooting. So far, it works like a charm.

I have absolutely no link with FusionPro, this is a pure disinterested advice, but feel free to delete if it violates forum rules.

XavierAM
  • 1,067
  • 9
  • 18