In this tutorial, I will show you how to run a Spring Boot app as a Windows Service. For this purpose, I will use the app developed in Basic REST API with Spring Boot 2023.
First, you need to add some properties in the build
section of the pom.xml
file. This will allow us to generate the jar file.
<build>
<!-- Name of the jar file -->
<finalName>HotelRestAPI</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Now, all you need to do is follow the next steps:
- Create a folder for the service. I will use
C:\HotelRestAPI - Service
. - Generate the jar file using
./mvnw clean install
from the Spring Boot App root directory. The jar file is generated inside thetarget
directory of the project. - Copy the jar file to
C:\HotelRestAPI - Service
. - Download WinWS and
sample-minimal.xml
. (I am going to useWinSW-x64.exe
). - Copy the
WinSW-x64.exe
andsample-minimal.xml
files toC:\HotelRestAPI - Service
. - Rename
sample-minimal.xml
toWinSW-x64.xml
. You can name the.xml
file whatever you want, but the.exe
file and.xml
file must have the same name. - Paste the following content in
WinSW-x64.xml
:
<service>
<!-- ID of the service.
It should be unique across the Windows system-->
<id>hotelrestapi</id>
<!-- Display name of the service -->
<name>Hotel REST API</name>
<!-- Service description -->
<description>
This service is the backend for the hotel web page
</description>
<!-- Path to the executable, which should be started -->
<executable>java</executable>
<arguments>-jar HotelRestAPI.jar</arguments>
</service>
Then, open a terminal as an administrator from C:\HotelRestAPI - Service
and run WinSW-x64.exe install
.
Now, go open the Services Panel and look up your service. Then, right-click your service and click Start.
Once the service is up and running some files will be generated in C:\HotelRestAPI - Service
.
Finally, you can check the service is working properly by making a request to http://localhost:8080/api/guests
.
If you need to uninstall the service, you can run WinSW-x64.exe uninstall
That's it. Now, you can run your Spring Boot apps as Windows Services. You can see all the source code used in this tutorial on my GitHub.
Thank you for reading.
Top comments (3)
Is that mean when start the windows this services automatically work and the server is running ?
Yes, you can configure it in the properties of the service from the Services Panel.
Okay, Thank you