Thumbnail image

Spring Boot: Running my app as Windows service

Wed, Feb 10, 2021 2-minute read

We know that we can run Spring Boot apps packaged as WAR files under Java application servers like Apache Tomcat. But we can even avoid such application server and run Spring Boot app directly. For this purpose we just need to build application as JAR file. Such JAR file can be run as an any other Java application by simply calling from console: ‘java -jar our-app.jar’ . That’s nice for testing purposes but for production environment we need to run this in some kind of service mode. On CentOS 7, for example, we know about systemd config files that define service to run.

On Windows platform we have ‘Services’ option as a way how to start application on background when OS booted. So how to start our Spring Boot app as Windows service?

To easily register/install our JAR application as system service we can use “winsw: Windows service wrapper” available here https://github.com/kohsuke/winsw . Its simple utility that manages our app service registration, starting, stopping etc. There are a few steps to use ‘winsw ‘ covered here:

Step 1: Download winsw — simply download https://github.com/kohsuke/winsw unzip, put exe file into directory with JAR file and rename to winsw.exe if not already.

Step 2: Create XML file with service description — create XML file with following(similar) content and name it ’winsw.xml‘ (the same name as winsws.exe). We have ‘service.jar’ file so we reference it with this name.

<service>
    <id>SpringBoot</id>
    <name>Spring Boot Demo</name>
    <description>Demo Spring Boot Windows service</description>
    <executable>java</executable>
    <arguments>-jar services.jar</arguments>
</service>

Step 3: Install service — simply by running ‘winsws install’ from command line in this directory. Just be sure you run ‘cmd’ as administrator user.

Step 4: Let’s check service exists — and open Windows services window. We should see our new service registered.

windows service

Step 5: Start service — from Windows services window we can start service as we usually do. We should see service running and we can check using web browser.

Step 6: Log files — are available in JAR file directory. Winsw wrapper creates a few log files:

  • winsw.wrapper.log — shows anything comming from wrapper itself
  • winsw.out.log — shows standard Spring Boot output as we can see e.g. in catalina.out under Apache Tomcat.
  • winsw.err.log — shows any application errors

Step 7: We updated our web application JAR file with a few more features — and would like to restart service. Simply replace JAR file with a new one and restart Windows service or use ‘winsw’ by calling ‘winsw restart’.

Our ‘winsw’ utility has a few others options.

wins