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
Please enter at least three characters to search
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • The Evolution of Scalable and Resilient Container Infrastructure
  • Building a Real-Time Audio Transcription System With OpenAI’s Realtime API
  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  • Traditional Testing and RAGAS: A Hybrid Strategy for Evaluating AI Chatbots
  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
Oops! Something Went Wrong

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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!