DEV Community

Javier Vega
Javier Vega

Posted on • Updated on

Spring Boot App as Windows Service

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>
Enter fullscreen mode Exit fullscreen mode

Now, all you need to do is follow the next steps:

  1. Create a folder for the service. I will use C:\HotelRestAPI - Service.
  2. Generate the jar file using ./mvnw clean install from the Spring Boot App root directory. The jar file is generated inside the target directory of the project.
  3. Copy the jar file to C:\HotelRestAPI - Service.
  4. Download WinWS and sample-minimal.xml. (I am going to use WinSW-x64.exe).
  5. Copy the WinSW-x64.exe and sample-minimal.xml files to C:\HotelRestAPI - Service.
  6. Rename sample-minimal.xml to WinSW-x64.xml. You can name the .xml file whatever you want, but the .exe file and .xml file must have the same name.
  7. 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>
Enter fullscreen mode Exit fullscreen mode

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.

Service Panel in Windows 10

Once the service is up and running some files will be generated in C:\HotelRestAPI - Service.

Files generated in the service directory

Finally, you can check the service is working properly by making a request to http://localhost:8080/api/guests.
Response of service

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)

Collapse
 
youssefa111 profile image
youssefa111

Is that mean when start the windows this services automatically work and the server is running ?

Collapse
 
javiervmc profile image
Javier Vega

Yes, you can configure it in the properties of the service from the Services Panel.

Image description

Collapse
 
youssefa111 profile image
youssefa111

Okay, Thank you