DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > 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.

Matthew Casperson user avatar by
Matthew Casperson
·
Jun. 12, 17 · Java Zone · Tutorial
Like (11)
Save
Tweet
18.14K 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.

Popular on DZone

  • Use Lambda Function URL To Write a Serverless App Backed by DynamoDB
  • Counting Faster With Postgres
  • What Do Great Engineering Managers Need To Know About Compensation and Equity?
  • How to Handle Early Startup Technical Debt (Or Just Avoid it Entirely)

Comments

Java Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo