Spring Boot Application as a Windows Service

Yiğitcan Nalcı
Javarevisited
Published in
4 min readJan 1, 2020

--

In this post, I will share with you how to run the Spring Boot application as a Windows Service.

These situations are very rare for Spring Boot applications but you can face like these. To accomplish this, you should read carefully what I wrote ;).

For running your application as a Windows service, you should do a few steps.

Maven Configuration

You can package your application as JAR or WAR. This is configured by pom.xml file like below. For this subject, we continue with JAR packaging.

<packaging>jar</packaging>

After that you should add spring-boot-maven-plugin artifact to your pom.xml.

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
pom.xml

As you see above, <executable> parameter is true. MANIFEST.MF file is added to the JAR package with this parameter. This manifest contains a Main-Class entry that specifies which class defines the main method for your application. If you don’t know Maven and want to learn about it, you can also check out these Maven courses

After these configurations are done, you should write below command to package your application as JAR.

$ mvn clean package

Now, you can test your application’s JAR package with the command below.

$ java -jar yourApp.jar

Windows Service Wrapper(WinSW)

For running a Java application as a Windows service, we need a tool called Windows Service Wrapper(WinSW). You can download this tool from this link. Also .Net Framework 4 must be installed in your machine for WinSW.

After downloading WinSW, you must follow the steps below.

  • Put the JAR file, WinSW executable and config in the same folder.
  • WinSW-xxx.xml file need to be configured correctly like below.
Directory of WinSW
  • The last two tags can remain the same. For our purpose, we just want to execute java -jar ./XXX.jar.
<configuration>
<!-- ID of the service. It should be unique accross the Windows system-->
<id>RC-XXX-SERVICE</id>
<!-- Display name of the service -->
<name>My Spring Boot App</name>
<!-- Service description -->
<description>Spring Boot Service</description>

<!-- we need to execute java -jar ./XXX.jar -->
<executable>java</executable>
<arguments>-jar "%BASE%\YOUR-APP.jar</arguments>
</configuration>

Install the Spring Boot Application as a Windows Service

  • After the configuration of XML file, you must open Windows CLI as administrator. You run the following two commands in succession.
Application installed as a Windows Service
$ winsw-2.2.0-net4.exe install
$ net start RC-XXX-SERVICE
Spring Boot app as a Windows Service
  • We install the application with first command. Second command starts the application. Also, you can start the application from Windows Services.

Thank you for reading! 🙏 Your thoughts are very valuable to me. Please feel free to share. 😄

Other Java and Spring articles you may like

  • 5 Spring Boot Features Every Java Developer Should Know (features)
  • Top 5 Free Courses to learn Spring and Spring Boot (courses)
  • 5 Course to Master Spring Boot online (courses)
  • 10 Things Java Developer should learn (goals)
  • 10 Tools Java Developers use in their day-to-day life (tools)
  • 10 Tips to become a better Java developer (tips)
  • Top 5 Courses to learn Microservices in Java? (courses)
  • 3 Best Practices Java Programmers can learn from Spring (best practices)
  • 5 courses to learn Spring Boot and Spring Cloud ( courses)
  • 3 ways to change Tomcat port in Spring Boot (tutorial)
  • 10 Advanced Spring Boot Courses for Java Programmers (courses)
  • 10 Spring MVC annotations Java developers should learn (annotations)
  • 15 Spring Boot Interview Questions for Java Programmers (questions)

Thanks for reading this article so far. If you find this article useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

--

--

Yiğitcan Nalcı
Javarevisited

Dad. Software Engineer. Sports lover, doer. Curious about science and technology. Novice writer for now. https://www.linkedin.com/in/yigitcannalci