By: Rick Proctor
Abstract: An example of how to turn a Java class into a Windows service using the open source Java Service Wrapper project.
Have you ever needed to run a Java class unattended on a Windows server? There
are various ways to accomplish this task. By far, the best alternative is to
run your Java class as a Windows service. One of the easiest tools I’ve
found for turning classes into services is the open source Java Service Wrapper
project from Tanukisoftware.org (http://wrapper.tanukisoftware.org/doc/english/index.html).
According to their website, the Java Service Wrapper is an application which
has evolved out of a desire to solve a number of problems common to many Java
In this article, we’ll focus on the first bullet point.
Note: There are three ways you can use the Java Service Wrapper.
In this article we will use the WrapperSimpleApp class to launch our application.
Following the link above, download and install the latest version the Java
Service Wrapper (for Windows users it comes in a zip file). For this example,
I used version 3.0.5. I extracted the file to my C: drive and it created the
following directory C:/wrapper_win32_3.0.5. Since I can’t know where you
are placing the files, for the rest of the article we’ll refer to the
wrapper home folder as %wrapper home%.
Looking at the tree structure, you’ll see several folders including:
The bin folder contains several files with a .bat.in extension. For this example,
you and I are only concerned with the App.bat.in file, the InstallApp-NT.bat.in
file, and the UninstallApp-NT.bat.in file. These files need to be renamed to
a .bat extension.
Once you’ve done that, run the App.bat from a command line. You should
see the following results:
Now that we know the test works, let’s make one of our own Java classes
The first thing we need to do is choose a Java class to turn into a service.
In this example, we’re going to use a class called MultipleSocket server.
The code for this class can be downloaded from CodeCentral by clicking
here. Once you’ve downloaded the code, find the bdn.jar file and move
it to the [wrapper folder]/bin directory.
On my PC I set up JAVA_HOME and CLASSPATH environmental variables as well as
provided a path to the %JAVA_HOME%/bin folder to make it easier to run java
classes from the command line. I’m going to assume that you’ve done
the same thing. If you haven’t, then you’ll need to provide the
full path to the java.exe file in the next examples.
Let’s test the bdn.jar file to make sure that everything’s ok.
At a command prompt, type:
java -cp bdn.jar bdn.MultipleSocketServer
After you hit the enter key you should see a screen similar to this:
Note: Use Ctrl-C to exit the program.
The next thing we need to do is to modify the wrapper.conf file. As mentioned
earlier, this file can be found in the %wrapper home%/conf directory. In this
file we are going to make the following changes:
And save the file.
Now let’s test our creation. At the command prompt run:
You should see the following:
Now that we’ve tested our class, let’s turn it into a fullfledged
service. At the command prompt run:
Let’s make sure that Windows recognizes the MultipleSocketServer class
as a Service. Click on the “services” icon, found in the administrative
tools option of the control panel. You should see a screen similar to this:
Notice that the MultipleSocketServer service is not started. To start it, you
can select it and click start, or you can run the App.bat file again. Once you’ve
started the MultipleSocketService, it will continue to run until you stop it,
disable it, or run the UnistallApp-NT.bat file. Congratulations…you have
just built a Java based Windows service.
The Java Service Wrapper from Tanukisoftware.org is an excellent open source
set of programs that allow you to turn your Java classes into Windows services.
It provides three methods for accomplishing this task. We discussed only one
method here, but I encourage you to read through the documentation and experiment
with the other methods as well.
Rick Proctor has over 20 years experience in the IT industry. Since 2001 Rick
has been Vice President of Information Technology for Thomas Nelson, Inc. (NYSE:
TNM). Rick can be reached at firstname.lastname@example.org.
The source code for MultipleSocketServer.java can be found at CodeCentral.
Server Response from: ETNASC01