Home : Running a Java Class as an Windows NT/2000/XP/Server 2003 Service
Q30411 - HOWTO: Running a Java Class as an Windows NT/2000/XP/Server 2003 Service

Running a Java Class as an Windows NT/2000/XP/Server 2003 Service

 

Background

Windows 2000 and Windows NT support the concept of services. A service is a software component that runs in the background. Examples include the "Event Log" service which provides logging facilities, and the "DHCP Client" service which manages obtaining an IP address for your machine. If you have IIS on your machine, it runs as a service also.

Service states are controlled by the services control panel (on Windows 2000 select Start->Programs->Administrative Tools->Services). Services respond to a number of control commands including start, stop, pause and resume. So a service must be programmed to handle these events.

The most interesting feature of services is that they are running even when there are no users logged on. So if your application needs to run all the time, with and without users logged on, then it must run as a service.

JavaService by Alexandria Software Consulting

JavaService is an application that allows any Java class to be run as a Service. It is an open source product that was originally developed and distributed by Alexandria Software Consulting.  However, the JavaService software is now hosted by the ObjectWeb source forge. It can be downloaded from here (http://javaservice.objectweb.org/index.html).

Please note that Intrinsyc Software does not provide technical support for JavaService.

We have successfully used this product to run the COM to Java bridges in our Application Server examples (such as the WebLogic example). In such a case, the bridge runs as a service, enabling Visual Basic clients to interact with EJBs via the bridge without having a user logged onto the server machine. Your server machine could even be a Server Applicance without a display.

The JavaService product provide excellent support for running a Java class as a service. To give you an idea of the features it supports, here is the usage output from the command line.

C:\>JavaService.exe /?
To install a service:
-install service_name jvm_library (jvm_option)*
-start start_class [-method start_method] [-params (start_parameter)+]
[-stop start_class [-method stop_method] [-params (stop_parameter)+>
[-out out_log_file] [-err err_log_file]

To uninstall a service:
-uninstall service_name

service_name: The name of the service.
jvm_library: The location of the JVM DLL under which to run the service.
jvm_option: An option to use when starting the JVM, such as:
"-Djava.class.path=c:\classes" or "-Xmx128m".
start_class: The class to load when starting the service.
start_method: The method to call in the start_class. default: main
start_parameter:A parameter to pass in to the start_method.
stop_class: The class to load when stopping the service.
stop_method: The method to call in the stop_class. default: main
stop_parameter: A parameter to pass in to the stop_method.
out_log_file: A file to redirect System.out into.
err_log_file: A file to redirect System.err into.

The only problem we encountered was with the classpath for the JVM. JavaService stores the classpath (and any other JVM parameters) in the registry, which is fine. When it reads this path however, it reads it into a character buffer which is only 256 characters long. So if you need to use a longer path, change the code by simply increasing the buffer size.

Note: We have received information that indicates this 256 char limit may be fixed as of JavaService v1.2.0.

Running COMtoWebLogic as a Service

Our WebLogic example runs a COM to Java bridge as a separate process. Here are the steps required to run that process as a Service. The procedure should be the same for any Java class.

1. Download and install JavaService.

2. Create a batch file (install.bat) with the following contents, updating the values of the environment variables to match your installation:

set JDKDir=c:\jdk1.3
set ServiceName=COMtoWebLogic
set JavaClassName=COMtoWebLogic

REM // The following is all on one line
JavaService.exe -install %ServiceName% %JDKDir%\jre\bin\hotspot\jvm.dll
-Djava.class.path=%CLASSPATH%
-DJINTEGRA_DCOM_PORT=7050
-start %JavaClassName%
-out c:\temp\%ServiceName%_stdout.log
-err c:\temp\%ServiceName%_stderr.log

3. Open the service control panel and start the service. Check in the application event log (on Windows 2000 select Start->Programs->Administrative Tools->Event Viewer) for error information. On my system the service appeared in the processes tab of the task manager as JavaService and consumed about 8MB of memory.

4. Note: To uninstall the service, close the event viewer and the service control panel if open and then execute JavaService.exe -uninstall COMtoWebLogic.

Troubleshooting

Here are some of the things you should check when things don't go right:

1. Check the Application Event Log (on Windows 2000 select Start->Programs->Administrative Tools->Event Viewer). If anything goes wrong for JavaService it places an entry in here.

2. Incorrect path to JVM.

3. Incorrect start class name. This is case sensitive.

4. Parameter lengths are > 256, update JavaService source if you think this is the problem. Note that you can view the parameters in the registry if you need to, they are located at HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\YourServiceName\Parameters.

5. Security - when services are installed the run under the system login called "LocalSystem". If your Java class needs to access some restricted resources (e.g. a COM server) then you will need to configure your service to login as a particular user. You can do this from the service control panel as follows (note, this description is for Windows 2000, NT is almost identical):

  a) select the service, right click and select properties.
  b) select the "Log On" tab.
  c) select the "this account" radio button.
  d) enter the appropriate user name and password
  e) hit ok.

The next time you start the service, it will run with the access permissions for of the specified user account.

Services which create user interface components (i.e. windows, message boxes, etc.) must be configured to interact with the desktop. This is done through the "Log On" tab of the properties for the service in the service control panel.

J-Integra support for Services

J-Integra does not provide any built-in support for Services. As it is a pure Java implementation it is not aware of such operating system specific features. We recommend using JavaService for this purpose.

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 6/23/2006.
Last Modified on 7/27/2006.
Last Modified by J-Integra KB Admin.
Article has been viewed 17745 times.
Rated 5 out of 10 based on 30 votes.
Print Article
Email Article