RSS

Make Java Application as Windows Service

08 Apr

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.

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.

New Project

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” on the left side page menu. 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 on “Files and Folders” on the left side page menu.

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 Go to the Services page by clicking on “Services” in the Project Details.

7.1 Install a new service

New Service Click on “Services to Install” and select “New Service Installation”. In the “Choose Instalation 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 Operation Right click on “Control Action” and select “New Service Operation”.

Edit the Service Name to: javaservice.exe.

Leave anything else unchanged.

Sevice Control Parameters

8. Build

Build Click on the [ Build ] toolbar button.

9. 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 Run it again and select Remove. That’s it!

Referenced by: http://www.advancedinstaller.com/user-guide/tutorial-java-service.html

Advertisements
 
Leave a comment

Posted by on April 8, 2010 in Java Pro

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: