1. How to write a Java Service
2. Build the Java Service Example msi package
3. Run the Java Service Example

1. How to write a Java Service

In order that the Java application to work correctly as a service you must implement 2 methods in 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.

The stop() method is called in another thread so you should implement if it is necessary a synchronization mechanism.


2. Build the Java Service Example MSI package

You have to choices:
 1. Open the project using AdvancedInstaller and click on build(Recommended)

 2. Using Ant(Advanced)
    - You must have Ant installed on your computer.
    
    - If you installed AdvancedInstaller in other directory than the default one you must modify ai.exe property in build.xml. The line in    

    build.xml looks like this:

    <property name="ai.exe" value="D:\work\advinst\advinst\win\Debug\AdvancedInstaller.exe"/>

    You must replace "D:\work\advinst\advinst\win\Debug" part from path.

    - open a command prompt: Click Start->Run
    In Run Dialog enter cmd.exe and press enter key
    In the command prompt enter:
    cd [directory where you unziped the Java Service Example]
    ant msi


3. Run the Java Service Example

 1. Install the msi package
 2. If the service do not start automatically open services applet
 Click Start->Run
 In Run Dialog enter services.msc
 3. Start service
 Right click on Java Service Example service and select start
 4. Test if it is working
 Open command prompt and enter: telenet localhost 4444
 You should see:
 Java Service is running

 Connection to host lost.
