Using JSVC to daemonize a Java app packaged with the Maven One-Jar Plugin

1.4k views Asked by At

Here is the problem:

I have packaged my Java application into a single jar using the Maven plugin One-Jar.

Now I want to run the application as a Unix Daemon using JSVC, i.e. Apache Commons Daemon.

I am using JSVC as follows (which works for Jars made with the Maven assembly plugin, etc):

jsvc -user $USER -home $HOME -pidfile $PID_PATH -cp $PATH_TO_ONE_JAR my.package.MyClass

The error is this:

jsvc.exec error: Cannot find daemon loader org/apache/commons/daemon/support/DaemonLoader
jsvc.exec error: Service exit with a return value of 1

Does anyone know if it is even possible to use JSVC and One-Jar together, since One-Jar uses a custom class loader? The jar runs just fine when I run java -jar my-one-jar.jar.

What can be done?

Thank you for any insight!

2

There are 2 answers

0
Andy MacKinlay On

If you use the (poorly-documented) Maven Shade Plugin instead of One-jar (they can achieve similar results as each other), it should solve your problems. It unpacks the dependent jars and stores the class files directly in the fat Jar (rather than having jars within the jar). I have used it to create an executable jar for running under JSVC with some success.

Of course, things are seldom as simple as they sound. With the Shade plugin, you may have to do some work to relocate classes when there are conflicts in your dependency tree, or use resource transformers to handle your non-Java resource files. But hopefully not.

(Of course Mkyong.com has a guide on this)

0
Pablo Carmona A. On

I had to add all jars dependencies to the classpath option from jsvc. It seems jsvc doesn't use the jars inside another jar