Creating a Java Service Installation

ImportantThe following article uses options that are available starting with the For Java edition and project type.

Let's say that we want to create a Windows Installer package for the included Java Service example. This tutorial will present in detail how to achieve this goal. To replicate it on your machine, you need to have a (latest) Sun JDK installed.

To start the tutorial, you must first download and unzip the Java Service Example. It contains all the files necessary to accomplish this tutorial, including a sample Advanced Installer project.

1. Prepare a Java application for running as a service

You have the choice of using the supplied example application or making your own. For a Java application to work correctly as a service you must implement two methods in its main class:

public static void main(String argv[])
  // implementation goes here

public static void stop()
  // implementation goes here

The main() method will be called when the service starts and, the stop() method will be called when service must stop, or the computer is shutting down. If you don't provide the stop() method which we recommend, Advanced Installer's native Java launcher will call the System.exit method instead.

2. Create project

Start Advanced Installer and choose “Java” > “Java Application” as the project type.

Start Page

Save A good idea is to save and name the project at this point. Use the “Save” menu item under the “File” menu or the Ctrl+S keyboard shortcut and let's name it javaserviceexample.aip.

3. Enter Product Details

Product Details

Please note that these details are completely fictive.

4. Set Install Parameters

Install Parameters Get to the Install Parameters page by clicking “Install Parameters” in the left pane. We won't change anything here, leaving everything to their default values.

5. Add Installation Files

Files and Folders Go to the Files and Folders page by clicking in “Files and Folders” on the left pane.

Add Folder Right click “Application Folder” and select “Add Files”. Browse to where you unzipped the Java Service example then go to the lib folder and select the Java Services example jar archive "javaservice.jar". Click OK.

Files, Folders, and Shortcuts

6. Define Java Products

Java Products Go to the Java Products page by clicking on “Java Products” on the left side page menu.

New Java Product Create a new [ Java Product ] by clicking on the New Product toolbar button. Let's call it javaservice.

Java Products

Click on the [ Add JAR(s)... ] button and browse to and select the javaservice.jar file. Click OK.

Edit the Main class to com.caphyon.service.JavaService.

At the application type select “Win32 Service”.

Product Settings

Now a new file has appeared in the Files and Folders Page: javaservice.exe.

NoteIf the service terminates unexpectedly from time to time, or when the user logs off, the Java Virtual Machine may receive incorrect signals. This can be fixed by using the Reduced usage of operating-system signals (-Xrs) option in the Edit JVM Parameters dialog (it can be launched from the Virtual Machine tab).

7. Define the Service

Services Select “Services” from “Resources” menu on the left pane.

7.1 Install a new service

New Service Right click on “Services to Install” and select “New Service Installation”. In the “Select Installed File Dialog” click on “Application Folder” and select "javaservice.exe".

New Java Service

7.2 Enter service properties

Leave Service Name unchanged.

Edit the Display Name: Java Service Example.

Edit the Description: This a Java Service Example.

Leave the Service Parameters, Service context and Service Account unchanged.

Installed Service Properties

7.3 Setup the control actions

New Service Control OperationSelect the newly created service control operation.

Edit the Service Name to javaservice.exe.

Leave anything else unchanged.

Service Control Parameters

8. Java Development Kit Prerequisite

Make sure you have the Java Development Kit(JDK) on the machine you want to deploy your Java service. The best way to do this is to add the JDK as a prerequisite to your package by going to the Prerequisite Page in Advanced Installer as selecting the Java Runtime Environment latest version.

NoteIf you plan only to test the Java service on your local machine and, you already have the JDK installed, this step is optional.

9. Build

Build Click on the [ Build ] toolbar button.

10. Run, test, and remove

Click on the [ Run ] toolbar button to run and test the MSI you created. After installation, you should have a new service installed.

To test the application open console and enter the following command: telnet localhost 4444. You should see:

Java Service is running

Connection to host lost

You also can build Java Service Example MSI package using ant. Open a console window and change current directory to the directory where you unzipped the Java Service example archive. Execute the following command: ant msi

To remove the installed package, simply press Run again and select Remove.

11. Video Tutorial

Java service installation