Spring Boot applications are versatile by nature and provide a lot of deployment possibilities. In this post, I am going to show you how to install a Spring Boot application as a Windows service. It is a fairly simple process that should take a couple of minutes 😉
1. Sample Application
For this demo, I have created a very simple Spring Boot application that writes a message to the console at a 5-second interval. But rest assured that any application will work just fine.
package rc.demo.winservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import java.util.GregorianCalendar; @SpringBootApplication @EnableScheduling public class WinServiceApplication { public static void main(String[] args) { SpringApplication.run(WinServiceApplication.class, args); } // enable a method to be called at a 5 second interval @Scheduled(fixedRate = 5000) public void doStuff() { System.out.println("Spring Boot Windows Service is running at " + new GregorianCalendar().getTime()); } }
I have also modified the pom.xml file to mark the application as executable.
<build> <plugins> <plugin> <configuration> <executable>true</executable> </configuration> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
After we create our app, we need to package it using Maven in order to obtain the JAR.
2. Download WINSW
In order to make our JAR capable of running as a Windows service, we need a little tool called winsw (Windows Service Wrapper), that can be downloaded from GitHub. At the time I am writing this article, the latest version is 2.12 and it can be downloaded from here.
Make sure that you also have .NET Framework 4 installed, as winsw needs it.
3. Put the JAR, winsw executable and config in a folder
Now we can start putting things together. Create a new folder and copy your JAR and the winsw executable. Now create an XML configuration file for winsw. The configuration file is needed to configure the Windows service parameters such as the name our service should have.
4. Edit the XML configuration file
With everything in one place, we can edit the WinSW.NET4.xml file. For our purpose, we just want to execute java -jar ./win-service.jar.
<configuration> <!-- ID of the service. It should be unique accross the Windows system--> <id>RC-SPRINGBOOT-SERVICE</id> <!-- Display name of the service --> <name>My Spring Boot App (powered by WinSW)</name> <!-- Service description --> <description>Long running Spring Boot service that prints the time every 5 seconds</description> <!-- we need to execute java -jar ./win-service.jar --> <executable>java</executable> <arguments>-jar "%BASE%\win-service.jar</arguments> </configuration>
5. Install the Spring Boot application as a Windows service
This is the final step of this process. We have everything configured. All that remains is to install our application as a service. To achieve this, we have to open the command prompt as Administrator and run the install command on winsw.exe. To start our service, we can then follow up with the net start command.
WinSW.NET4.exe install net start RC-SPRINGBOOT-SERVICE
6. The log files
Now that our application has started as a Windows service, we might want to see the status of the service or the messages that the application prints. We can do this by navigating to the folder where we installed the app. Some new files were created:
– WinSW.NET4.wrapper.log that contains info about the service installation
– WinSW.NET4.out.log that contains all the prints/logging that our application is performing
– WinSW.NET4.err.log that contains error messages captured from our application
7. Video Tutorial
You can also see the whole process in action in my YouTube tutorial.