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.
-
1Not in the registry also? Then why would you want it as a Service?o.k.w– o.k.w2009-10-24 09:17:49 +00:00Commented Oct 24, 2009 at 9:17
-
@o.k.w is right if you are creating windows service, It'll create entry in registry.Ravi Parekh– Ravi Parekh2014-09-10 11:28:27 +00:00Commented Sep 10, 2014 at 11:28
-
I think this user meant "run as a service, not from the startup registry keys as a non service"Warren P– Warren P2016-09-14 13:54:58 +00:00Commented Sep 14, 2016 at 13:54
-
2You can directly create an exe or msi file from java using javapackager tool with java 8 onwards. Refer: stackoverflow.com/questions/68113/…Steephen– Steephen2017-08-24 02:00:07 +00:00Commented Aug 24, 2017 at 2:00
7 Answers
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>"
6 Comments
unexpected status SERVICE-PAUSED in response to START controlnssm install <servicename> and then manage all options (there is a lot) interactively.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.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.
2 Comments
nssm install <servicename> and set the AppDirectory as well as other useful parameters such as log files there and create the service in one goI'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.
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.
Comments
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.
3 Comments
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
Comments
[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.