How to create a Windows Service for Spring Boot Application by Procrun

Procrun is a set of applications that allow Windows users to wrap (mostly) Java applications (e.g. Tomcat) as a Windows service. The service can be set to automatically start when the machine boots and will continue to run with no user logged onto the machine.

The tutorial will guide you how to create a Windows Service for Spring Boot Application by Procrun.

I. Technologies – create a Windows Service for Spring Boot Application by Procrun

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE
– Procrun: commons-daemon-1.0.15

II. Overview
1. Structure of Project

springbootwindowsservice-procrun-overview

2. Step to do

– Create Spring Boot project
– Add needed dependencies
– Re-implement the Entry point of Spring Boot Application
– Implement a Bootstrap class for Start & Stop windows-service function
– Create a simple web controller
– Configure build plugin & start-class
– Download procrun
– Build install, un-install, run, stop Procrun command-line for Spring Boot Windows Service
– Run & Enjoy result

III. Practices
1. Create Spring Boot project

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info as below images:

springbootwindowsservice-procrun-create project

Press Next then Finish, spring boot project is created successfully.

2. Add needed dependencies

Open pom.xml file, add dependencies.

3. Re-implement the Entry point of Spring Boot Application

For start/stop function of a Windows-Service, re-implement entry point class:

4. Implement a Bootstrap class for Start & Stop windows-service function

Create a Bootstrap class that extends JarLauncher. The class will implement 2 function start & stop for Windows-Service.

5. Create a simple web controller

6. Configure build plugin & start-class

Open pom.xml file, configure start-class:

Configure Maven build plugin:

7. Download procrun

Download Procrun from: Commons-Deamon

8. Build install, un-install, run, stop Procrun command-line for Spring Boot Windows Service

– Create install.bat file

– Create uninstall.bat file

– Create run.bat file

– Create stop.bat file

– Use echo %PROCESSOR_ARCHITECTURE% for determined the appropriate procrun.exe

– Place all files: install.bat, uninstall.bat, run.bat, stop.bat and prunsrv.exe in a procrun folder.

9. Run & Enjoy result

-Maven build project with goals:
clean install

– Open cmd on windows with mode:administrator privileges, cd to procrun folder.

– Run install.bat – for register a Window Service
– Run run.bat – for start the Windows-Service

springbootwindowsservice-procrun-service running

Make request:
http://localhost:8080/hello

springbootwindowsservice-procrun-request

– Run stop.bat: for stop Windows-service
– Run uninstall.bat: for un-register Windows-Service

IV. Source code

SpringBootWindowsService

By JavaSampleApproach | September 6, 2016.


Related Posts


1 thought on “How to create a Windows Service for Spring Boot Application by Procrun”

Got Something To Say:

Your email address will not be published. Required fields are marked *

*