How to create windows service from java jar?
Solution 1
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>"
Solution 2
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.
Solution 3
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.
Solution 4
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.
Solution 5
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.
Comments
-
Rakesh Juyal over 3 years
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.
-
Ravi Kumar Gupta almost 11 yearsis there any example that you can share?
-
Ravi Kumar Gupta almost 11 yearsThanks BTakacs, I already checked your tutorial and it worked fine. But unfortunately that did not solve what I wanted to do.. I am trying to achieve that now from C#.
-
BTakacs almost 11 yearswell, in that case you should check that answer: stackoverflow.com/a/15115104/566006
-
aneela over 9 yearsI did same and it installed successfully but as soon as I run it, it says
unexpected status SERVICE-PAUSED in response to START control
-
mcdon over 9 yearsFor a Windows service WinRun4j is also a good candidate. It can double as a Java Launcher, or Service Wrapper.
-
krzysiek.ste almost 9 yearsThis same problem as you mentioned. Any idea what is wrong?
-
EvilInside almost 9 yearsSame problem as you guys mentioned..any updates on the solution?
-
Destroyica over 8 yearsYou can run
nssm install <servicename>
and then manage all options (there is a lot) interactively. -
martin jakubik over 7 yearsThis is good... You should add it as a comment to kopernik's answer.
-
martin jakubik over 7 yearsIf you don't want to run as Administrator, see gcerkez's answer below.
-
JCvanDamme almost 7 yearsSetting the AppDirectory after creation of the service failed in my case. But you can launch a gui with
nssm install <servicename>
and set the AppDirectory as well as other useful parameters such as log files there and create the service in one go -
Tim B James almost 6 yearsLate 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. -
Wisteso over 4 yearsBe warned: the documentation is awful for Apache Commons Daemon