Run Java Program as a Windows Service With Procrun


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:

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:

set SERVICE_NAME=Subversion-Updater
set PR_DISPLAYNAME=Subversion Updater Service
REM Full path of prunsrv.exe 
set PR_INSTAL=C:\svn-updater-service\prunsrv.exe
REM Service log configuration
set PR_LOGPATH=d:\logs
set PR_STDOUTPUT=d:\logs\stdout.txt
set PR_STDERROR=d:\logs\stderr.txt
REM Path to java installation
set PR_JVM=C:\Program Files\Java\jre1.8.0_51\bin\server\jvm.dll
REM this jar is supposed to be located in the prunsrv.exe directory 
set PR_CLASSPATH=Subversion-Updater.jar
REM Startup configuration
set PR_STARTUP=auto
set PR_STARTCLASS=com.codesenior.subversion.service.Main
set PR_STARTPARAMS=CodeseniorComments;3600000
REM Shutdown configuration
set PR_STOPCLASS=com.codesenior.subversion.service.Main
REM JVM configuration
set PR_JVMMS=64
set PR_JVMMX=128
set PR_JVMSS=128
set PR_JVMOPTIONS=-Duser.language=TR;-Duser.region=tr
REM Install service
prunsrv.exe //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: 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:


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