66

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.

4
  • 1
    Not in the registry also? Then why would you want it as a Service? Commented Oct 24, 2009 at 9:17
  • @o.k.w is right if you are creating windows service, It'll create entry in registry. Commented 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" Commented Sep 14, 2016 at 13:54
  • 2
    You can directly create an exe or msi file from java using javapackager tool with java 8 onwards. Refer: stackoverflow.com/questions/68113/… Commented Aug 24, 2017 at 2:00

7 Answers 7

58

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>"
Sign up to request clarification or add additional context in comments.

6 Comments

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
This same problem as you mentioned. Any idea what is wrong?
Same problem as you guys mentioned..any updates on the solution?
You can run nssm install <servicename> and then manage all options (there is a lot) interactively.
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.
|
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.

2 Comments

This is good... You should add it as a comment to kopernik's answer.
Setting 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
12

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.

2 Comments

For a Windows service WinRun4j is also a good candidate. It can double as a Java Launcher, or Service Wrapper.
Be warned: the documentation is awful for Apache Commons Daemon
9

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

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.

3 Comments

is there any example that you can share?
Thanks 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#.
well, in that case you should check that answer: stackoverflow.com/a/15115104/566006
4

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

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.