How to run any Java Application as Windows Service?

In this article we will learn how to run any Java Application as Windows service.

As we know that, to run any java jar we have a simple command as below

java -jar <JarFileName.jar>

Or running spring boot application using maven plugin as following

mvn spring-boot:run

These are the manual practices and valid to run the application temporarily or development purpose. Once user close/cancel terminal or console or system shut down or restarted the application will be closed automatically.

There are several ways to keep the Application Up and running as windows service. Windows Service Wrapper approach is one of widely used.

In this article we will discuss about the first approach Windows Service Wrapper. In short it’s called ‘winsw ‘.

Its simple utility that manages our app service registration, starting, stopping etc. There are a few steps to use ‘winsw ‘ covered here.

#Prerequisite :


Before we start make sure you have the following

  • Java Application Jar file - That you want to run as windows service

#Step 1: Download winsw


Download latest winsw binaries from here & put the .exe file to the same directory with java jar file.

Rename the file to winsw.exe

#Step 2: Create XML file with service description


Create XML file winsw.xml with id, name, description etc.

Add jar file name in arguments tag of xml.

winsw.xml

<service>
    <id>DemoJavaApp</id>
    <name>Demo Java App</name>
    <description>Demo Java App Windows service</description>
    <executable>java</executable>
    <arguments>-jar demo-java-app.jar</arguments>
</service>

#Step 3: Install Service


Open command line (cmd) as administrator user and go to the directory location with jar file.

Type following command to install service

winsws install

#Step 4: Check the service


Check the service in windows services list.

#Step 5: Start the service


Right click on Demo Java App the service and click on start.

#Step 6: Checking Logs


Log Files are available in JAR file directory. Winsw wrapper creates a few log files:

  1. winsw.wrapper.log — shows anything comming from wrapper itself
  2. winsw.out.log — shows standard Spring Boot output as we can see e.g. in catalina.out under Apache Tomcat.
  3. winsw.err.log — shows any application errors

#Un-installing an Installed Application


To un-install already installed app, Use following command

winsws uninstall

#Other Options provided by winsw


‘winsw’ utility has a few others options as following

C:\winsw>winsw -h
A wrapper binary that can be used to host executables as Windows services

Usage: winsw [/redirect file] <command> [<args>]
       Missing arguments trigger the service mode

Available commands:
- 'install'   - install the service to Windows Service Controller
- 'uninstall' - uninstall the service
- 'start'     - start the service (must be installed before)
- 'stop'      - stop the service
- 'restart'   - restart the service
- 'restart!'  - self-restart (can be called from child processes)
- 'status'    - check the current status of the service
- 'test'      - check if the service can be started and then stopped
- 'testwait'  - starts the service and waits until a key is pressed then stops the service
- 'version'   - print the version info
- 'help'      - print the help info (aliases: -h,--help,-?,/?)

Extra options:
- '/redirect' - redirect the wrapper's STDOUT and STDERR to the specified file

WinSW 2.3.0.0
More info: https://github.com/kohsuke/winsw
Bug tracker: https://github.com/kohsuke/winsw/issues


Thank You. Happy Learning!!!