Run Java application as Windows Services

Sometimes you need to execute a java program as the machine starts without the needing to log-in. To achieve that you need to make a Windows Service.

For the purpose of this short how to, I will assume you have a folder with aRunnableJarFile.jar with maybe a lib folder and a configuration file for the java application, which is the usual way for packaging Java applications

One option, and the one I like the most is to use the open source project at Java Service Launcher, it gives you two important files. jsl.exe and jsl.ini. you normally put those files in the same folder as yourRunnableJarFile.jar.
You edit the jsl.ini file so as to tell jsl.exe how to launch your runnable jar ( e. g. cmdline= -jar aRunnableJarFile.jar ), you can also configure where the jvm is, where will be the working directory for your application as the service starts, where you'll redirect all the output or erroutput of the sevice to a file and things like that.
As you have the jsl.ini configured you can run your application with 'jsl.exe -debug' which will launch your java app rigth in the console so you can see if everything went ok with the jsl.ini, you can also run 'jsl.exe -install' and it will create a windows service pointing to that jsl.exe file.
As you start the windows service ( or normally configure it to automatically start ) it will launch the jsl.exe file wihch will run your java application. When you stop the windows service jsl will call System.exit() in you app, so any shutdown hooks that you configured will be called by default. It is really easy.

Another option is to create a exe wrapping your jar file. JSmooth is an open source project that does exactly that.
It has a graphic user interface that walks you though wizard-like steps to create an exe wrapper for your Java application and as you create the wrapper, you tell jsmooth that you want the wrapper to be a windows service.
It produces aRunnableFileWrappingYourJarFile.exe which you can run with an 'install' or 'remove' argument to install or remove the application as a windows service.

Although I didn't see something similar to jsl.exe, there are lots of programs like jsmoth but mostly commercial.

Hope you can find useful this post and any problems you have I'll do my best to help you

P.D.: please forgive my bad English


Veera said...

Very useful information.

dleskov said...

Excelsior JET has an option to compile your app to a Windows service. It also provides a WinService API enabling service pause/resume, writing to the Event Log, etc.

Anonymous said...

Thanks for the info - did not know about this alternative before. However, I still find the good old Java Service Wrapper at way more useful. It's available for several platforms (not only Windos) and has proven me stable service over the years.

Anonymous said...

I like exe4J. It is a commercial application but it can handle Java applications for GUI, console or Windows service. It includes a wiazrd to create the appropriate application type.
So, I didn't mind paying the small fee for exe4J given that it provides so many features.


SBL Software said...

Useful Info!

SBL Software Development Solutions