How to run spring boot app as windows service (winsw)

Step 1: Download WinSW

  1. Go to the WinSW GitHub Releases page.

  2. 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 to MyApp.exe.

Step 3: Create a Configuration File

  1. 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.
  2. 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

  1. Open a Command Prompt as Administrator.

  2. Navigate to the directory containing the .exe and .xml files.

  3. Run the following command to test:

     MyApp.exe install
    
  4. Start the service:

     MyApp.exe start
    
  5. To check the status of the service:

     MyApp.exe status
    
  6. Stop the service if needed:

     MyApp.exe stop
    

Step 6: Verify the Service

  1. Open the Windows Services Manager:

    • Press Win + R, type services.msc, and hit Enter.
  2. Look for your service by the name you specified in the XML file.

  3. 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:

    1. Open the Services Manager (services.msc).

    2. Right-click your service and select Properties.

    3. 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 like MyApp.out.log and MyApp.err.log.

  • Check these files for any errors if the service does not start correctly.