Autostart for your Spring Boot Application

A few months ago I was asked to find a solution for starting and stopping a Spring Boot application under Windows automatically together with the computer this application was running on. After doing some research I found a nice fitting and open source solution with WinSW.

As you can read on the Github page of WinSW it “is an executable binary, which can be used to wrap and manage a custom process as a Windows service”. This windows service can be used to automatically start/stop your application on computer startup/shutdown. After downloading the binary (you can find it here) you have to perform the following simple steps to install your own custom windows service.

Step 1: Name the Service

First you take the downloaded winsw-2.1.2-bin.exe file and rename it to the name of your service. In this example I will call this MyCustomService.exe.

Step 2: Configure the Service

Next, you have to create a new MyCustomService.xml file and place it right next to the executable (it is mandatory that the file name is the same). This xml file holds all the configuration for your custom windows service. It could look like the following example:

<service>
    <id>MyCustomService</id> <!-- must be unique -->
    <name>MyCustomService</name>
    <description>This service runs my custom service.</description>
    <executable>java</executable>
    <arguments>-jar "%BASE%\myCustomService.jar"</arguments>
    <logpath>%BASE%\log</logpath>
    <log mode="roll-by-time">
    <pattern>yyyyMMdd</pattern>
    <download from="http://www.example.de/spring-application/myCustomService.jar" 
        to="%BASE%\myCustomService.jar"
        auth="basic" unsecureAuth="true"
        user="aUser" password="aPassw0rd"/>
    </log>
</service>

This configurations basically tells the windows service to:

  1. Download the jar file from the given URL and place it in the current folder
  2. Execute the just downloaded jar by executing the command java -jar myCustomService.jar
  3. Save all logs into the log folder (for more details about logging click here)

Step 3: Install the Service

To finally install the service as a Windows service you open your command line in the current folder and execute MyCustomService.exe install. After the installation you can directly test your service by executing MyCustomService.exe test. Now you can manage this service like any other default windows service. To put it in the autostart you have to navigate to your Windows services, select the newly service and set the Startup type to Automatic.

Conclusion

As seen in this short example WinSW can be used not only for executing java programs automatically on Windows startup but also for updating your programs automatically. In case you need to update this jar file on multiple Windows clients this can be a pretty neat feature, because you only have to replace the jar hosted on http://www.example.de/spring-application/myCustomService.jar and restart the computers.

David Klassen

Recent Posts

Typesafe HTTP Clients with OkHttp and Retrofit

Developers use HTTP Clients to communicate with other applications over the network. Over the years, multiple HTTP Clients have been developed to suit various application needs.

Read more

Reactive Architecture with Spring Boot

Microservices are meant to be adaptable, scalable, and highly performant so that they can be more competitive to the other products in the market.

Read more

Comprehensive Guide to Java Streams

A stream is a sequence of elements on which we can perform different kinds of sequential and parallel operations. The Stream API was introduced in Java 8 and is used to process collections of objects.

Read more