JNT is a wrapper that allows you to run your Java application as an NT service. JNT works only with Java applications, and offers some unique features:
See the file ‘usage.txt’
or type JNT without parameters to see the full command line syntax for
JNT. This quick guide will focus only
on installing and uninstalling a Java NT Service.
To install a Java
application as an NT service, use this form:
JNT “/InstallAsService:Service Name[:depend1, depend2, …]” [/SDstartingDirectoryPath] <java options and commands>
Note: things in
brackets (‘[‘ and ‘]’) are optional.
The optional “depend”
list allows you to specify services on which this service is dependent. If you have two services and one depends on
the other to start first, then you could do something like this:
JNT “/InstallAsService:Dependent Service:Main Service” ...
This command line tells JNT to install the Java application
as the service named “Dependent Service”, and that this service depends on the
NT service named “Main Service” to be started first.
The /SD parameter is optional, and allows you to specify the
starting directory of your service.
Windows NT defaults to starting a service with the “system32” directory as
the service starting directory.
For <java options and commands>, you can use any java
command line you wish (minus the ‘java’, of course). Here is an example:
JNT “/InstallAsService:Test” “/SDc:\Program Files\Test” –Dtest.property=Test com.ews.Test
Note: JNT will use the default currently installed JVM. There is currently no way to tell JNT which
JVM to use if you have multiple JVMs installed.
To uninstall a service, use this command line:
JNT “/RemoveAsService:Service Name”
Notes:
You can use JNT to install as many services on the same
machine as desired.
JNT will create a file in the start directory of the service
named, “JNT.log”. This is a simple log
file that allows you to track when JNT starts/stops and service, and the java
command it uses to do so. Also note
that JNT will send events to NT that can be viewed by the NT Event Viewer when
the service is installed, started, stopped, uninstalled, or the service
parameters are changed.
When JNT receives the service stop event, it will look in the JVM for a system property named “shutdown.method”. If this property is set to a value, then JNT will then attempt to find a static method in your “main” class with the name specified in the “shutdown.method” property. This method must be public, static, return nothing (void), and take no parameters. Here is an example prototype:
public static void stopApplication();
It is totally acceptable to call System.exit(#) in this
method. In fact, System.exit() is
permissible anywhere in your application.
JNT will properly handle any call to System.exit() and inform NT that
the service has stopped. Of course, you
do not need to call System.exit(). If
this method returns normally then JNT will wait for the “main” method of the
application to return and for all non-daemon threads to stop before shutting
down the service.
NOTE: it may be necessary to use System.exit() to shutdown
the service if your application creates background non-daemon threads. An example would be if your application uses
RMI. In this case it may be necessary
to use System.exit(); otherwise the service will never respond to the stop
signal as there will still be background threads running when the “main” method
returns. Also note that if this method
throws an exception of any kind, then the JVM will be terminated and the
service will exit.
If “shutdown.method” is not set, then JNT will have no
choice but to kill the JVM and exit the service when the service stop signal is
received.
Here is an example command line that installs a test
application as an NT service and specifies a shutdown method by setting the ‘shutdown.method’
system property:
JNT “/InstallAsService:Test” “/SDc:\Program Files\Test” –Dshutdown.method=stopApplication –cp “c:\Program Files\Test\test.jar” com.ews.Test
In this example, ‘public static void stopApplication()’ must appear in the class of ‘com.ews.Test’.