Windows Service - Apache commons daemon class not found

3.4k Views Asked by At

I have a Jar file that needs to be installed as a windows service. I'm using Apache commons daemon procrun.

Though the service appears in the Services tab in task manager its not running. Its in stopped state and attempting to start it seems not working.

*In commons daemons logs I noticed that it cannot load the main class which is the "AgentApp".

I installed 32bit jre too.

Mentioned here under the log file contents.

imageprocagent stderr:

2015-01-07 15:47:47 Commons Daemon procrun stderr initialized
java.lang.NoClassDefFoundError: org/apache/commons/daemon/Daemon
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.daemon.Daemon
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 12 more
Exception in thread "main" 

crawford-daemon:

[2015-01-07 15:47:47] [info]  Commons Daemon procrun (1.0.10.0 32-bit) started
[2015-01-07 15:47:47] [info]  Running 'ImageProcAgent' Service...
[2015-01-07 15:47:47] [info]  Starting service...
[2015-01-07 15:47:49] [error] FindClass com/fx358/fx9/agent/AgentApp failed
[2015-01-07 15:47:49] [error] Failed to start Java
[2015-01-07 15:47:49] [error] ServiceStart returned 4
[2015-01-07 15:47:49] [info]  Run service finished.
[2015-01-07 15:47:49] [info]  Commons Daemon procrun finished
  • `[2015-01-07 15:47:49] [error] FindClass com/fx358/fx9/agent/AgentApp failed

Here is the script I'm using to install the agent:

SET DISPLAYNAME="ImageProcAgent"
SET DESCRIPTION="FX9 image processing Agent"
SET LOGPATH="C:\FX9\logs\ImageProcAgent"
SET STARTPATH=
SET JVMLOCATION="C:\Program Files (x86)\Java\jre7\bin\client\jvm.dll"
SET CUSTCLASSPATH="C:\FX9\release\ImageProcAgent\GenericAgent_jar\GenericAgent.jar;C:\FX9\release\ImageProcAgent\GenericAgent_jar\ImageProcAgent.jar;."
ECHO ON
ImageProcAgent.exe //IS//ImageProcAgent --DisplayName=%DISPLAYNAME% --Description=%DESCRIPTION% --Startup=auto --LogPath=%LOGPATH% --LogLevel=INFO --LogPrefix=crawford-daemon --StdOutput=auto --StdError=auto --StartPath=%STARTPATH% --StartClass=com.fx358.fx9.agent.AgentApp --StartMethod=main --StartParams=start --StartMode=jvm --StopPath=%% --StopClass=com.fx358.fx9.agent.AgentApp  --StopMethod=stop  --StopMode=jvm --Jvm=%JVMLOCATION% --Classpath=%CUSTCLASSPATH% --JvmOptions=-Xmx512M  ++JvmOptions=-Djava.util.logging.config.file=logging.properties

` Can someone point me out what I'm doing wrong? thanks in advance.

3

There are 3 best solutions below

0
On

In your classpath declaration there are only 2 jar entries:

CUSTCLASSPATH="C:\FX9\release\ImageProcAgent\GenericAgent_jar\GenericAgent.jar;C:\FX9\release\ImageProcAgent\GenericAgent_jar\ImageProcAgent.jar;."

Is your application's your build as a fat-jar containing all compile and runtime dependencies? Otherwise you'll have to add each JAR that is needed at runtime of your service to the classpath. For example:

CUSTCLASSPATH="C:\FX9\release\ImageProcAgent\GenericAgent_jar\GenericAgent.jar;C:\FX9\release\ImageProcAgent\GenericAgent_jar\ImageProcAgent.jar;C:\FX9\release\ImageProcAgent\GenericAgent_jar\lib\commons-daemon-1.0.15.jar;<PROBABLY_MORE_JARS_NEED_TO_BE_ADDED_HERE>;."

Furthermore using . in the classpath of your service is probably not a good idea. You should only use fully qualified paths in there. Otherwise you'll be dependent on the location from where the service is executed by Windows.

0
On

I had the same error. Turns out, I was attempting to use commons-logging and log4j directly within the Service class. However, the log4j was not initializing properly, thus causing the load to error out.

I removed all the log4j, replacing it with a simple System.out.* and it worked.

0
On

I had the same error with a runnable jar build with ANT. The Service-Class fails with a severe error. One reason was, i used signed libraries SHA1-Digest, please check this. To avoid this problem don't build a runnable, deliver the libraries you are using, and set the Class-Path in the MANIFEST.MF of your JAR-File. Hope this helps you.