What's new

How to Convert a Java Program Into a Windows Service

ebenzunlimited

Moderator
Java is a widely used language in which many programs are being written. Windows is the most widely-used operating system and there are many occasions when a Java program needs to run as a windows service. This article explains how a java program can be run as a windows service. There are many libraries and programs available that can be easily used to wrap a Java program into a windows service. This article describes use of free software Java Service Launcher (for Windows) to run a Java program into a windows service.
Instructions

    Program info and getting the Service Launcer
        1

        Note down which class (say com.sample.EntryClass) in the java program is the entry point for the application and has the main method.
        2

        Note down the location of the java archive(jar file) which contains the java program.

        Sponsored Links
            Analyze C# code coverage

            View code coverage in Visual Studio Coverage highlighting and reports
            jetbrains.com/dotcover
        3

        Download Java Service Launcher (JSL) from http://jslwin.sourceforge.net/
        4

        Extract the JSL software to a local directory.
        5

        Copy JSL.EXE and JSL.INI anywhere on the disk, preferably to the location noted in Step 2 where the java program is located.
    Configuring and installing the Service
        6

        Configure JSL.ini with the correct parameters. The main parameters to configure are described in following steps.
        7

        Set the service name and description by setting the variables 'appname', 'servicename', 'displayname', 'servicedescription' appropriately.
        8

        Set the account and password to be used for the windows service using the variables 'account' and 'password.'
        9

        Assign the class, method and parameters that will be called when the service is stopped by setting the variables 'stopclass', 'stopmethod' and 'stopparams.'
        10

        Set up the parameters to configure the class and method to be called when the service is paused by setting 'pauseclass', 'pausemethod', 'pausesignature' and 'pauseparams.'
        11

        Set the java runtime to be used for running the program using by setting the variable ?jrepath.?
        12

        Set the working directory which be used for running the java program by setting the variable ?wrkdir?. This will mostly be the location noted in Section 1, Step 2.
        13

        Configure the java program to be run as a service by setting the variable ?cmdline?. The class will be the class noted in Step1. Example: cmdline = -cp %P1%\\src com.sample.EntryClass
        14

        Alternate to Step 8 above: Use the params variable to configure the java program that will be run as a windows service.
Example :
params = 3
param00=-cp
param01=C:\\myapp\\lib\\lib.jar
param02= com.sample.EntryClass
        15

        Execute the command jsl.exe ? install. This installs the service that has been configured in the previous steps.
        16

        Start the windows service just installed by using windows service manager.

 
Top