DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Trending

  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • AI's Dilemma: When to Retrain and When to Unlearn?
  1. DZone
  2. Coding
  3. Frameworks
  4. Deploying Spring Boot Apps as Windows Services

Deploying Spring Boot Apps as Windows Services

There are a few hurdles to jump over to get Spring Boot apps up and running as Windows services. Here, we use Octopus Deploy and a couple of custom tools.

By 
Matthew Casperson user avatar
Matthew Casperson
·
Jun. 12, 17 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
20.3K Views

Join the DZone community and get the full member experience.

Join For Free

A customer recently asked if it was possible to deploy a Spring Boot application as a Windows Service using Octopus Deploy. The Spring documentation does briefly mention a method for running Spring Boot applications as Windows services, but a lot of the details are left to the reader to figure out. So in this blog post, I'll show you how to quickly run a standard Spring Boot application as a service.

Java and Windows Services

There are two things we need to run a Spring Boot UberJAR as a Windows service.

The first thing is an executable that Windows can actually run as the service. This is provided by the winsw project. Winsw is not specifically tied to Java, but it can be used to execute java.exe, which is all we need in order to start our Spring Boot JAR file.

The second thing is some way to gracefully shut down a Java application running in the background. For that, we have a simple project called the Spring Boot Stopper. This application will communicate with a Spring Boot application via JMX and instruct it to shut down.

Putting it All Together

This demo project is a fairly stock Spring Boot REST MVC application generated using the Spring Initializr website. The project hasn't been configured in a special way, and when built, will produce a stock Spring Boot JAR file.

In the dist folder of the project, you will find a number of files.

  • SpringBoot.exe: which the winsw executable renamed.
  • SpringBoot.exe.config: the winsw EXE configuration file.
  • SpringBoot.xml: the winsw XML configuration file.
  • SpringBootStopper.jar: the Spring Boot Stopper JAR file.
  • SpringBootWindowsService.jar: the Spring Boot JAR file.

The XML configuration file is where most of the magic happens. Inside you will find the following settings:

<executable>java</executable>
<startargument>-Dspring.application.admin.enabled=true</startargument>
<startargument>-Dcom.sun.management.jmxremote.port=50201</startargument>
<startargument>-Dcom.sun.management.jmxremote.authenticate=false</startargument>
<startargument>-Dcom.sun.management.jmxremote.ssl=false</startargument>
<startargument>-jar</startargument>
<startargument>SpringBootWindowsService.jar</startargument>


These settings define what the winsw executable will run when the Windows service is started. In this case, we are starting the Spring Boot UberJAR with some additional system properties that configure JMX. Here, we enable the Spring Boot admin features, set the JMX port to 50201, disable SSL, and disable JMX authentication.

We then have some additional settings to define what the winsw executable will run when the Windows service is stopped.

<stopexecutable>java</stopexecutable>
<stopargument>-jar</stopargument>
<stopargument>SpringBootStopper.jar</stopargument>
<stopargument>50201</stopargument>


Here, we run the SpringBootStopper.jar application, passing in the same JMX port that was used when starting the Spring Boot application. These settings allow SpringBootStopper.jar to connect to the running Spring Boot instance and gracefully shut it down.

Packing Up the Service

To package these files up for Octopus Deploy, use the CLI tool.

Octo.exe pack --format=zip --id=SpringBootWindowsService --version=1.0.0


This will produce a file called SpringBootWindowsService.1.0.0.zip, which you can then push to the Octopus server with the command:

Octo.exe push --server=http://my.octopus.server --apiKey=API-xxxxxxxxxxxxx --package=SpringBootWindowsService.1.0.0.zip


Deploying the Service

At this point, you can deploy the package as a traditional Windows service executing the SpringBoot.exe executable.

Deploy Windows Service

Once deployed, the service will appear like any other Windows service.

Windows Service

Conclusion

Start the service up, and open a browser to http://localhost:8080/greeting. Your Spring Boot application is now running as a Windows service!

Spring Framework Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: