Social Media

Category Archives for Uncategorized

procrun – Java Programs as Windows Services

I recently needed to run a Java program as a Windows service, and opted for Commons-daemon procrun. This wrapper is used by both Tomcat and JBoss Wildfly to wrap their servers – but took a bit of figuring out how to get my application running.

This post sets out an example of using procrun to wrap a Java process.

Download

I downloaded procrun from download. The download contains 3 different version of the procrun.exe –

  • 32 bit – this is the default architecture
  • amd64 – 64-bit AMD architecture
  • ia64 – 64-bit Intel Itanium architecture

You need to use the right version for your JVM and chipset

Code

The code is based on the EchoServer and EchoClient examples from oracle.

EchoServer

EchoClient

The client is changed to take a shutdown parameter –

Prunssrv

Ive also created a simple class to stop and start the server –

Putting it all together –

  1. Add the above classes and procrun.exe to a directory – C:\procrun
  2. Compile – javac *.java
  3. Create Archive – jar cvf simpleechoserver.jar *.class *.jar

service.bat

You dont need to create a service.bat file – but its cleaner and simpler. Store this in your code directory.

Key Points

  • All the Procrun fields are marked with PR_ – you can also feed these fields directly to procrun.exe using the ++ or — notation in the procrun notes, but i think this way is cleaner and easier to maintain
  • The key ones are the start/stop fields
  • PR_JVMOPTIONS – allows us to pass system properties to the Windows Service
  • Installing and removing –
    %PR_INSTALL% //IS//%SERVICE_NAME%
    %PR_INSTALL% //DS//%SERVICE_NAME%
  • There are other “//” options defined in the notes

Running service.bat

You may need to run this as administrator –

To install –

And uninstall –

You can then test –

If you go to your Windows Services you will now see SimpleEchoServer with stop/start/restart options

prunmgr.exe

The final trick is to use prunmgr. This the the procrun manager and allows you to see the procrun operating parameters. To get started go to your copy of prunmgr.exe and rename or copy it to the SERVICE_NAME in your batch file

So –

You then run the SimpleEchoServer.exe as administrator –

simpleechoserver

Reference

https://commons.apache.org/proper/commons-daemon/procrun.html

1 2 3 46