5

I am trying to get a Java application to run as a Linux service using jsvc. I found How to convert a java program to daemon with jsvc? which was able to get me most of the way, but I am still struggling to get the classpath figured out.

I am getting the following errors to stderr:

19/04/2013 10:43:18 12233 jsvc.exec error: Cannot find daemon loader org/apache/commons/daemon/support/DaemonLoader

19/04/2013 10:43:18 12231 jsvc.exec error: Service exit with a return value of 1

It would seem that the runtime is unable to find the commons-daemon.jar.

My application is structured so that with the application itself in a single jar file, with dependencies, including commons-daemon in a lib directory.

  • daemon-script.sh
  • myapp.jar
  • lib/commons-daemon.jar
  • lib/other-jars

Here is the relevant parts of my daemon-script.sh:

LIB_DIR=$(pwd)/lib/*

CLASS_PATH=$(pwd)/myapp.jar

$EXEC -home $JAVA_EXEC -cp $CLASS_PATH:$LIB_DIR -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS

I have tried numerous variations: relative path, specifically referencing lib/commons-daemon.jar, no wildcard, etc. Does anyone have an idea how to make this work properly?

Also, myapp.jar is a self-executable jar (mostly for testing purposes, and yes, I still need it to run as a service), so the manifest.mf contains the Class-Path and Main-Class attributes. Is there a way to get jsvc to recognize the manifest?

Community
  • 1
  • 1

1 Answers1

2

You can use multiple jars with JSVC using : between them.

For your specific issue the solution would be changing the CLASS_PATH variable to this:

CLASS_PATH=$(pwd)/myapp.jar:$(pwd)/lib/commons-daemon.jar:$(pwd)/lib/other-jars

Alternatively you can include all jars in a directory by using wildcards like this:

CLASS_PATH=$(pwd)/myapp.jar:$(pwd)/lib/*.jar

Hope this helps

Adrian Lopez
  • 1,776
  • 1
  • 17
  • 35
  • 1
    My mileage varied using jsvc 1.0.8 (on Ubuntu). jsvc would not respect the wildcards or just specifying the lib/ dir. Instead, I had to construct the classpath explicitly using "CLASS_PATH=$(echo "$LIB_PATH"/*.jar | tr ' ' ':')". I also ran into a snag trying to specify -Dlog4j.configuration. I usually just specify an absoluate path to the properties file, but to get it to work with jsvc, I had to write it as a URL with the file:/// prefix. I'll be testing on RHEL soon as well. – ayang Jan 09 '14 at 19:37