Java - Javaservice - Creation of a Windows Service
Table of Contents
Introduction
On Windows, when the machine fails and/or is rebooted, your application is unavailable for remote commands until a user logs in and starts it.
As an solution, you can configure your process :
- as a Windows service that will run automatically when the machine boots.
- as a scheduled task.
The most common method used is to install it as service with JavaService.
Articles Related
Syntax and parameters of javaservice
JavaService (Windows NT Service Daemon) commands:-
-version
-licence
-install service_name jvm service_options...
-queryconfig service_name
-status service_name
-uninstall service_name
-help
-help topic
But you have more options that you can found on this page JavaService installation parameters.
You can redirect for instance the log file with this parameters:
- -out out_log_file (optional) - A file into which System.out will be redirected. If this parameter is not specified, System.out will not be redirected.
- -err err_log_file (optional) - A file into which System.err will be redirected. If this parameter is not specified, System.err will not be redirected.
Installation Steps
Download
Download the open source tool named JavaService.exe from http://javaservice.objectweb.org/ which can create Windows Services for Java Programs.
install
Adjust the path and execute the following command:
javaservice ^
-install "My Service Service" "PathToJDK32Bit\jre\bin\client\jvm.dll" ^
-XX:MaxPermSize=128m ^
-Xmx512m ^
"-Djava.class.path=PathToYouJar/app.jar" ^
-start theMainClass ^
-out "pathToTheOutStreamLog.txt" ^
-err "pathToTheErrorStreamLog.txt" ^
-description "App description"
Ref: Metalink
Check the configuration in the registry
Start the regedit which is in the directory C:\WINDOWS\SysWOW64 if you are in a 64 bit environement
HKLM\SYSTEM\CONTROLSET001\Services
Support
Unrecognised or incorrectly-ordered parameters for install command
If you receive this error during the installation, be careful with the order of the parameters. The description must be at the end of the command for instance.
Unrecognised or incorrectly-ordered parameters for install command
The last 2 parameters (from '-description') were not recognised
JavaService install command parameters not valid, or incomplete
LoadLibrary is not a valid Win32 application
If you receive this error during the start of the service (check the Application/Event Viewer)
The LoadLibrary function failed for the following reason: LoadLibrary is not a valid Win32 application.
It's because in 64 bit, you must use a 64 java version and not a 32 bit.
The specified module could not be found
If you receive this error during the start of the service (check the Application/Event Viewer)
The LoadLibrary function failed for the following reason: The specified module could not be found.
It's because the path that you have specified during the installation are not good. You deinstall the service with the uninstall.bat file or you can modify them in the registry.
Others possibilities
Windows Service with NT/Win2k Resource Kit
Answer from the Oracle OC4J FAQ, only for 32 Bit machines
How can I start OC4J standalone as a service in NT ?
In Oracle9iAS Release 2, OC4J is started as a managed process by Oracle Process Management Notification (OPMN). This provides automatic startup, shutdown and death detection functionalities for OC4J processes. For environments which require the use of OC4J as a service, Oracle9iAS should be used.
Optionally, you can set OC4J as a service in Windows as follows:
- Create a command file to start OC4J as follows:
set PATH=c:\java1.3\bin;%PATH
cd \oracle\ias\j2ee\home
java -jar oc4j.jar
- Create a service for the this command file using NT/Win2k Resource Kit. You can use the following Microsoft article how to create a service: http://support.microsoft.com/support/kb/articles/q137/8/90.asp
This solution is only good for 32bit machine because Windows do not support srvany.exe (in the ressource Toolkit on a Windows 2003 64-bit machines.