How to use Apache Commons Daemon with Cross-Platform Service?

5k Views Asked by At

I have a Java daemon (system service running 24/7/365) that needs to deploy as an executable JAR, however I would like users (on Windows, Linux and Mac alike) to be able to start/stop the application via:

service myapp start

service myapp stop

(Or whatever is the equivalent for Windows/Mac services). Obviously, this requires something at the OS-level to map the myapp "service" to a particular method call from inside my Java app (perhaps, LifecycleManager#start() and LifecycleManager#stop() respectively).

I heard that Apache Commons Daemon can be used for this purpose, and after perusing their site, it looks like it does just this. However it looks like it's an old project and there really isn't any documentation for accomplishing what I am looking for.

So I ask: can commons-daemon do what I need, or do I need something else or in addition? How can I get a cross-platform daemon out of an executable JAR? Thanks in advance!

1

There are 1 best solutions below

0
On

Yes, Apache Commons Daemon can run your jar as a service on Windows (using procrun) or as a daemon on *NIX (using JSVC). I only used it on Windows, so the rest only applies to procrun:

Procrun supports proper Windows service shutdown (it can invoke a stop method in your running application). It also has a little bit of extra functionality like optionally redirecting your stdout and stderr to separate log files, and running the service wrapper exe directly (assuming you renamed it to your service name) runs a non-service mode console that lets you see the output immediately. You probably want to use procrun's "jvm mode". Read the details of how to do that in the documentation.

If you need additional fancier capabilities, such as if the service needs to be able to restart itself, take a look at YAJSW (Yet Another Java Service Wrapper) instead. YAJSW is quite likely better overall, but I haven't tried it yet.