Run Java Program as a Windows Service With Procrun

04-09-2015

What is Apache Commons Deamon Procrun?

Procrun is a set of applications that allow Windows users to wrap (mostly) Java applications (e.g. Tomcat) as a Windows service. The service can be set to automatically start when the machine boots and will continue to run with no user logged onto the machine.

You can download Procrun binaries here: https://downloads.apache.org/commons/daemon/binaries/windows/commons-daemon-1.2.1-bin-windows.zip

Create Service

Lets create a Subversion-Updater service.

1. Create Windows service from command promt (Administrator privilages)

prunsrv //IS//Subversion-Updater --Startup=auto --DisplayName="Subversion Updater" --Install=C:\svn-updater-service\prunsrv.exe ^
--Classpath=Subversion-Updater.jar --Jvm="C:\Program Files\Java\jre1.8.0_51\bin\server\jvm.dll" --StartMode=jvm --StopMode=jvm ^
--StartClass=com.codesenior.subversion.service.Main --StopMethod=stop  --StopClass=com.codesenior.subversion.service.Main ^
--StartMethod=start --LogPath=d:\logs --LogPrefix=Subversion-Updater --LogLevel=Info --StdOutput=d:\logs\stdout.txt ^
--StdError=d:\logs\stdout.txt --JvmMs=64 --JvmMx=128 --JvmSs=128 ^
--JvmOptions=-Dfile.encoding=UTF8 ++JvmOptions=-Duser.language=TR ++JvmOptions=-Duser.region=tr

Notice that we wrapped Jvm option parameter with double quota:

--Jvm="C:\Program Files\Java\jre1.8.0_51\bin\server\jvm.dll"
Also note that if you have more than one jvm options you should add with ++ letters. First jvm option starts with --
--JvmOptions=-Dfile.encoding=UTF8 ++JvmOptions=-Duser.language=TR ++JvmOptions=-Duser.region=tr

2. We can also create a batch file containing following content:

@echo off
setlocal

set SERVICE_NAME=E-Belge-Imza

if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
 set PR_INSTALL=%~dp0%\amd64\prunsrv.exe
) else (
set PR_INSTALL=%~dp0%prunsrv.exe
)

set PR_DESCRIPTION=E-Belge E-Imza Servisi

REM Service log configuration
set PR_LOGPREFIX=%SERVICE_NAME%
set PR_LOGPATH=%~dp0%\
set PR_STDOUTPUT=%~dp0%\stdout.txt
set PR_STDERROR=%~dp0%\stderr.txt
set PR_LOGLEVEL=Debug
 
REM Path to java installation
set PR_JVM=C:\Program Files (x86)\Java\jre1.8.0_241\bin\client\jvm.dll
set PR_CLASSPATH=e-imza-1.0.0.jar
 
REM Startup configuration
set PR_STARTUP=auto
set PR_STARTMODE=jvm
set PR_STARTCLASS=gov.telifhaklari.eimza.Main
set PR_STARTMETHOD=main
set PR_STARTPARAMS=%~dp0%\resources

REM Shutdown configuration
set PR_STOPMODE=jvm
set PR_STOPCLASS=gov.telifhaklari.eimza.Main
set PR_STOPMETHOD=stop
set PR_STOPTIMEOUT=120
 
REM JVM configuration
set PR_JVMMS=256
set PR_JVMMX=1024
set PR_JVMSS=4000

REM current file
set "SELF=%~dp0%service.bat"
REM current directory
set "CURRENT_DIR=%cd%"
 
REM start - This takes the input from installService and places it between x's
REM       - if there are none then you get xx as a null check
if "x%1x" == "xx" goto displayUsage
set SERVICE_CMD=%1
REM ahift moves to next field
shift
if "x%1x" == "xx" goto checkServiceCmd
:checkServiceCmd
if /i %SERVICE_CMD% == install goto doInstall
if /i %SERVICE_CMD% == remove goto doRemove
if /i %SERVICE_CMD% == uninstall goto doRemove
echo Unknown parameter "%SERVICE_CMD%"
:displayUsage
echo.
echo Usage: service.bat install/remove
goto end
:doRemove
echo Removing the service '%PR_INSTALL%' '%SERVICE_NAME%' ...
"%PR_INSTALL%"  //DS//%SERVICE_NAME%
if not errorlevel 1 goto removed
echo Failed removing '%SERVICE_NAME%' service
goto end
:removed
echo The service '%SERVICE_NAME%' has been removed
goto end
:doInstall
echo Installing the service '%PR_INSTALL%' '%SERVICE_NAME%' ...
"%PR_INSTALL%" //IS//%SERVICE_NAME% 
goto end
:end
echo Exiting service.bat ...
cd "%CURRENT_DIR%"
Very Important: If you want to install service in Program Files, you should escape space character by using quota at last line: "%PR_INSTALL%" //IS//%SERVICE_NAME%

After created a batch file, call the file from command prompt (Administrator Privilages) and don't double click the batch file!

Note: If C:\Program Files\Java\jre1.8.0_51\bin\server\jvm.dll is not working, install JAVA JRE 32 bit. I installed Java 32 bit because of an error.

C:\Program Files (x86)\Java\jre1.8.0_241\bin\client\jvm.dll

Notice that there is no server folder in bin folder of 32 Bit installation folder.

Note: We should set full path of prunsrv.exe file in PR_INSTALL at line 3. Classpath value is relative to prunsrv.exe location. Therefore no need to give full path of the jar file. At this configuration, we accept prunsrv.exe file and Subversion-Updater.jar file is at the same location.

The basic Service definitions are maintained under the registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<ServiceName>

Additional parameters are stored in the registry at:

HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\ProcRun 2.0\<ServiceName>\Parameters

On 64-bit Windows procrun always uses 32-bit registry view for storing the configuration. This means that parameters will be stored inside:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\ProcRun 2.0\<ServiceName>

Java Tab

Log Tab

Start Tab

Stop Tab

Note: Service name cannot contain space. Also note that you should run above commands in command prompt as administrator

Remove Service

To uninstall created service, run following command:

prunsrv.exe //DS//{Service Name}

For example:

prunsrv.exe //DS//Subversion-Updater

Sometimes created service can turn into disabled mode and command cannot delete the service. If this happens, follow below steps to completly remove it:

1. Start cmd in elevated mode. Then type: sc queryex <SERVICE_NAME> .Then you'll get some info. A PID number will show.

2. Run taskkill /pid <SERVICE_PID> /f command where /f is to force stop

© 2019 All rights reserved. Codesenior.COM