How to run spring boot app as windows service (winsw)
Step 1: Download WinSW
Go to the WinSW GitHub Releases page.
Download the latest
.exe
file (e.g.,WinSW-x64.exe
).
Step 2: Rename the .exe
File
Rename the downloaded
.exe
file to match your application name. For example:- If your Spring Boot application is named
MyApp
, rename it toMyApp.exe
.
- If your Spring Boot application is named
Step 3: Create a Configuration File
In the same directory as your renamed
.exe
file, create an XML configuration file with the same name as the.exe
file, but with an.xml
extension. For example:MyApp.xml
.
Add the following content to the XML file:
<service>
<id>MyApp</id>
<name>My Application</name>
<description>This is my Spring Boot application running as a Windows service.</description>
<!-- Path to the Java executable -->
<executable>C:\path\to\java.exe</executable>
<!-- Path to your Spring Boot JAR -->
<arguments>-jar C:\path\to\your-spring-boot-app.jar</arguments>
<!-- Optionally define working directory -->
<workingdirectory>C:\path\to</workingdirectory>
<!-- Logging -->
<logmode>roll</logmode>
</service>
Replace
C:\path\to\java.exe
with the path to your Java installation.Replace
C:\path\to\your-spring-boot-app.jar
with the full path to your Spring Boot JAR file.
Step 4: Place Your Spring Boot JAR File
- Ensure your Spring Boot application's JAR file is located at the specified path in the XML file.
Step 5: Test the Configuration
Open a Command Prompt as Administrator.
Navigate to the directory containing the
.exe
and.xml
files.Run the following command to test:
MyApp.exe install
Start the service:
MyApp.exe start
To check the status of the service:
MyApp.exe status
Stop the service if needed:
MyApp.exe stop
Step 6: Verify the Service
Open the Windows Services Manager:
- Press
Win + R
, typeservices.msc
, and hit Enter.
- Press
Look for your service by the name you specified in the XML file.
Start or stop the service from the Services Manager to confirm it works.
Step 7: Optional - Configure Automatic Startup
Set the service to start automatically:
Open the Services Manager (
services.msc
).Right-click your service and select Properties.
Change the Startup type to Automatic.
Step 8: Logs and Troubleshooting
Logs are typically stored in the same directory as the
.exe
file, with files likeMyApp.out.log
andMyApp.err.log
.Check these files for any errors if the service does not start correctly.