Java
Windows Service
Software Development
Programming Tutorial
Java Application

How to create a Windows service from Java app

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Creating a Windows service from a Java application is a common requirement for developers who need their applications to run in the background, typically during system startup without user intervention. This article explains the process of converting a Java application into a Windows service, including step-by-step guidance and examples.

Prerequisites

Before we dive into the technical details, ensure that you have the following prerequisites:

  • Java Development Kit (JDK): Make sure you have a suitable version of the JDK installed on your system.
  • Windows Operating System: The steps and tools used here are specific to Windows.
  • Java Application: Your existing Java application ready to be turned into a service.

Understanding Windows Services

Windows services operate in the background and are managed by the Service Control Manager. They are ideal for applications that need to start automatically, run continuously even when the user is not logged on, and require minimal user interaction.

Tools for Creating Windows Services

1. Java Service Wrapper

The Java Service Wrapper is a utility that allows Java applications to be run as a service. It is a popular solution supported across different platforms.

2. Apache Commons Daemon (Procrun)

Apache Commons Daemon provides Procrun, a set of applications that can handle the creation of native services for Java applications on Windows.

Steps to Create a Windows Service Using Apache Commons Daemon (Procrun)

Here’s a practical approach using Procrun:

Step 1: Download and Extract Apache Commons Daemon

First, download Procrun from the Apache Commons Daemon website. Extract the package to a desired location on your system.

Step 2: Set Up Your Java Application

Ensure your Java application is packaged into a JAR file, e.g., YourApp.jar. Your application needs a main class with a public static void main(String[] args) method, as this serves as the starting point of your application.

Step 3: Create a Batch File for Service Installation

Create a batch file, installService.bat, with the following content to define the service:

batch
1@echo off
2
3set SERVICE_NAME=YourAppService
4set PR_INSTALL=%~dp0prunsrv.exe
5set PR_SERVICE_BIN=%~dp0YourApp.jar
6set PR_JAVA_HOME=C:\path\to\your\java\home
7
8%PR_INSTALL% //IS//%SERVICE_NAME% ^
9  --Description "Your Java Application as a Windows Service" ^
10  --ClassPath "%PR_SERVICE_BIN%" ^
11  --StartMode=jvm ^
12  --StartClass=your.main.Class ^
13  --StartParams=start ^
14  --StopMode=jvm ^
15  --StopClass=your.main.Class ^
16  --StopParams=stop ^
17  --Jvm=auto ^
18  --JvmOptions=-Djava.class.path="%PR_SERVICE_BIN%;" ^
19  --JvmOptions=-Xmx256m

Step 4: Run the Batch File

Execute the installService.bat script to install the application as a Windows service. Open a command prompt in administrator mode, navigate to the folder containing your batch file, and run:

batch
installService.bat

This command installs your Java application as a Windows service.

Step 5: Manage Your Service

After installation, manage your service using standard Windows service management tools. Use services.msc to start, stop, or configure your service.

Additional Management Commands:

  • To Start the Service:
batch
  sc start YourAppService
  • To Stop the Service:
batch
  sc stop YourAppService
  • To Uninstall the Service:
batch
  %~dp0prunsrv.exe //DS//YourAppService

Advantages of Java Windows Services

  • Autonomous: Runs without user interaction.
  • Resilient: Recovers from failures automatically using service recovery options.
  • Scalable: Can be configured for different environments easily.

Summary Table

FeatureDescription
AutostartCan be configured to start with the system boot.
Background ExecutionRuns independently from user sessions.
ResilienceAutomatically restarts on failure using service controls.
Resource ConfigurationConfigurable memory and CPU limits through JVM options.
Multiple PlatformsSolutions such as Java Service Wrapper and Procrun support various OS.

Conclusion

Running a Java application as a Windows service enables it to operate smoothly in environments requiring high availability and minimal user intervention. Tools like Apache Commons Daemon simplify this process, allowing developers to focus more on application logic than on deployment concerns. By following these steps, you can establish a stable background service on Windows to support your Java applications.


Course illustration
Course illustration

All Rights Reserved.