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. In order to replicate it on your own machine, you need to have a (latest) Sun JDK installed.
Before you start the tutorial 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
- 2. Create project
- 3. Enter Product Details
- 4. Set Install Parameters
- 5. Add Installation Files
- 6. Define Java Products
- 7. Define the Service
- 8. Build
- 9. Run, test and remove
1. Prepare a Java application for running as a service
You have the choice of using the supplied example application or making your own. In order for a Java application to work correctly as a service you must implement 2 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.
2. Create project
Start Advanced Installer by selecting it from the “Start” menu. Launching Advanced Installer automatically starts a new installation project. Choose “Java” > “Java Application” as the project type.

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

Please note that these details are completely fictive.
4. Set Install Parameters
Get to the Install
Parameters page by clicking “Install Parameters” on
the left pane page menu. We won't change anything here, leaving
everything to their default values.
5. Add Installation Files
Go to the Files and
Folders page by clicking on “Files and Folders” on
the left pane page menu.
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.

6. Define Java Products
Go to the Java Products page by clicking on
“Java Products” on the left side page
menu.
Create a new
by clicking on the New Product toolbar
button. Let's call it javaservice.

Click on the 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”.

Now a new file has appeared in the Files and Folders Page: javaservice.exe.
If 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
Select
“Services” from “Resources” menu
on the left pane.
7.1 Install a new service
Click on “Services
to Install” and select “New Service
Installation”. In the “Choose Installation
Dialog” click on “Application Folder”
and select "javaservice.exe".

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.

7.3 Setup the control actions
Select the
newly created service control operation.
Edit the Service Name to: javaservice.exe.
Leave anything else unchanged.

8. Build
Click on the
toolbar button.
9. Run, test and remove
Click on the 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 lostYou 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 Run it again and select Remove. That's it!
The End
This concludes our tutorial.