bitrazor.com ...THE source for mediocre content                                      

TiVo HME How-To: Make your App Installable as a Windows Service             

To maximize your application's usability, you really want it to be running all the time in the background, and you want it to start up whenever the computer starts up.  On Windows, this can be a little problematic for Java applications.  This page describes how to use the commercial package exe4j ($70) to wrap your application as a Windows service.

I will use the example of my TrafficCam Viewer application.

First, prepare your application for deployment like you normally would.  Test it to make sure it runs manually.

Screen Shots as I Set Up the Project

Because most of my apps have config files and other directories, I must use regular mode:

The "Short name of your application" is what shows up as the name of the service in the Services control panel.

The Distribution source directory contains all your files.  Don't worry, the end-user won't have to install the app in the same location.

The Executable name  is the name of the .exe that exe4j will generate when you're all done here.

From the screen above, choose the 'Advanced Options' drop-down, and pick 'Redirection':

This is really nifty -- choose log files for stdout and stderr.  If you choose to put them in a directory, like I do below, make sure the directory exists once your app is installed by the end-user.  If the directory's not there, the service will silently fail to start up, leaving you frustrated and confused.  Or maybe that was just me. :)

The description below shows up in the Services control panel.

For auto start or start on demand, I like auto start.  The user can always change it later in the Services control panel.

This stuff's nice, but optional:

Note that the name of your entry point (com.bitrazor.tc.TrafficCam, in my example) goes into the 'Arguments' box, not the 'VM Parameters' box.

For the Class path, you can either add each JAR to the list manually.  However, I prefer to stick them all in a 'jars' directory (or I suppose 'lib' might be more traditional), then use the 'scan directory' option.  This works well, and it has the major advantage of being easy to maintain when the next release comes around.

Too lazy to create a splash screen:

These are all the default messages; I didn't change any of them.

Your Service .exe File

When you get done with all of this, you get an .exe file generated.  In my case, it's called TrafficCam_Viewer_Service.exe (see the third screen shot).  Note that this executable has four switches that are useful:

  • /install
  • /uninstall
  • /start
  • /stop

Those do what you think they'd do to the service.  For convenience, I create 4 corresponding batch files for my users, which I include in my distribution  Here's an example:

trafficcam_service_install.bat
@echo off
@echo.
@echo Installing TrafficCam Viewer as Windows Service:
@echo.
TrafficCam_Viewer_Service.exe /install
TrafficCam_Viewer_Service.exe /start
@echo.
pause

My Deployment

That's pretty much it.  All that's left is to zip it all up and stick it on the web for my users.  Here's what my deployment source directory looks like:

I just go up one directory (to 'trafficcam' in this example), and zip it.  Test it, then release it.

Windows Firewall Exception

[Added 8/6/05] In addition, you should also see 'How to Create a Windows Firewall exception for your Service'.

 

 

                                                         Last updated: August 7, 2009