Spring Boot App as Windows Service
Tags: springboot, beginners, api, windows
Published: 2023-09-08 07:35:24

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](https://dev.to/javiervmc/basic-rest-api-with-spring-boot-2023-3bkh).

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.

```xml
<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:

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](https://github.com/winsw/winsw/releases) 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`:

```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.

![Service Panel in Windows 10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7cy4tco6zmt9yjvbubgk.png)

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

![Files generated in the service directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1f53m8m4z5c2b052ip6v.png)

Finally, you can check the service is working properly by making a request to `http://localhost:8080/api/guests`.
![Response of service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndsytfy5mr0haiz6650c.png)

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](https://github.com/JavierVMC29/Spring-Boot-Hotel-REST-API).

Thank you for reading.