Sunday, November 2, 2008

Creating Windows NT Services for a Java application


Creating Windows NT Services out of a Java application

There are many ways of creating Windows Services out of Java Applications, one very popular of them is by using the Java Service Wrapper (a set of batch files) developed by Tanuki Software. Below are the steps which one need to follow to install, launch, stop, or uninstall Windows NT services for a Java app:-
Download the utility

wrapper.exe is the actual executable of the utility named Java Service Wrapper which you need to download from the Tanuki Software web site. The distribution comes in three flavors - Professional, Standard, and Community. The first two are required to be purchased whereas the third is freely downloadable. You can download the distribution for your platform here.

Download and set up the Scripts

The Java Serive Wrapper distribution (freely downloadable) contains three batch files as well. These files are initially named: App.bat.in, InstallApp-NT.bat.in, UninstallApp-NT.bat.in which you need to rename to make them suitable for your application. The Batch files doesn't need any other change except for the rename until you comply with where to put these files and where to keep the configuration directory and its contents. We'll see how easily can these tasks be done.

Suppose your application mame is 'MyApp' then you would be required to rename the three batch files as MyApp.bat, InstallMyApp-NT.bat, and UninstallMyApp-NT.bat respectively. Notice that there is no '.in' extension now. Please do make sure that you rename the batch files correctly.

In the default case you need to place these batch files in the same directory where you have kept the wrapper.exe file in the previous step.

Setting up the Configuration File

There is one configuration file named wrapper.config for this utility which you need to put in a directory named 'config' located one level up than the location which contains the wrapper.exe file and the three batch files.

Can we place the files at different locations?

Yeah... you surely can. But, in that case you will need to modify the batch files and the configuration file accordingly. The modifications are quite easy to make as you'll simply be required to update the default values of the variables storing the various pathnames.

Running the application in Console Window

Once the set up is done then you would first like to check if your application is actually ready to be installed as a service or not. This you can ensure by running the application in a console window first. Just double-click the MyApp.bat file and the application will start in a console window. Like any other console execution this can also be terminated by Ctrl+C whenever required. Wondering how did the batch file get the application name? From the name of the batch file itself - that's why you were required to change the names of batch files in the beginning.

C:\MyApp\bin>MyApp.bat
wrapper | --> Wrapper Started as Console
wrapper | Launching a JVM...
jvm 1 | Wrapper (Version 3.x.x)
jvm 1 |

Installing the Service

So now that your application has already been tested in a console window, you would like it to be installed as a Windows NT service. You are just a click awaya and if everything is okay the application will be installed as a Windows NT service almost instantly. If something goes wrong you'll be notified the actual errors. Just fix them and try again in that case. The service will also have the same name 'MyApp'.

C:\MyApp\bin>InstallMyApp-NT.bat
wrapper | My Application installed.

Starting the installed Windows NT Service

You can either start them by going through Control Panel -> Administrative Tools -> Services -> Start the service OR by executing the command 'net start MyApp' OR by using the utility 'wrapper.exe'. You can setthe property wrapper.ntservice.starttype to AUTO_START and in that case the service will be started whenever the machine is rebooted.

To start the service by using the 'wrapper.exe' utility you need to execute the command 'wrapper.exe -t ..\conf\wrapper.conf'.

C:\MyApp\bin>wrapper.exe -t ..\conf\wrapper.conf

Stopping an already started Windows Service

Again you can either do that either through the Control Panel -> Admin Tools -> Services -> Stop OR by executing the command 'net stop MyApp' OR by executing the utility 'wrapper.exe' with the proper switch '-p' in this case.

C:\MyApp\bin>wrapper.exe -p ..\conf\wrapper.conf

Why to prefer 'wrapper.exe' with corresponding switches for Start/Stop?

Because 'net start MyApp' and 'net stop MyApp' may work well only for those services which don't take much time to get started or stopped. If a service is taking much time while stopping, the execution of 'net stop ServiceName' may show 'Service stopped successfully' even before the service actually stops. This might be a problem if the execution of the application is dependent on the start/stop status of the service. You may face a similar issue with 'net start ServiceName' as well. Do make a note that Control Panel -> Admin Tools -> Services doesn't have these kind of problems and hence you may rely on them.

Using 'wrapper.exe' with appropriate switches ('-t' for Start and '-p' for Stop) guarantees that you don't face any such issue. Another advantage is that you don't need to specify the ServiceName in this case. It picks the name of the service from the configuration file.

Uninstalling an installed Service

Want to get rid of the service? You can do that with the same ease. You just need to run the UninstallMyApp-NT.bat file. You would see something similar to this:

C:\MyApp\bin>UninstallMyApp-NT.bat
wrapper | Service is running. Stopping it...
wrapper | Waiting to stop...
wrapper | My Application stopped.
wrapper | My Application removed.

Installing/Uninstalling the service using 'wraper.exe' with switches

You can install and uninstall Windows NT services by executing the wrapper.exe file with appropriate switches as well. '-i' is the switch for installing a Windows NT service and '-r' is the switch for removing (uninstalling) it. You can find the list of all available switches and their usage by executing 'wrapper.exe' without passing any argument. It'll show the Usage pattern of the utility, all the switches and their respective meanings.

Checking the Status of the Service

'wrapper.exe' when executed with the '-q' switch will show the current status of the Service showing if the installed or not and if installed then its Start Type (whether Automatic or not), whether the service is Interactive or not, and whether the service is Running currently or not.

Liked the article? You may like to Subscribe to this blog for regular updates. You may also like to follow the blog to manage the bookmark easily and to tell the world that you enjoy GeekExplains. You can find the 'Followers' widget in the rightmost sidebar.



Share/Save/Bookmark


No comments: