title: Error while running Daemon class in java?
body: <p>I am trying to make a Daemon class in java so I have implemented a class which implements Daemon but when I run it, I always get errors. </p>
  
  <p>The class that I use for that is ;</p>
  
  <pre><code>public class RemoteAgentDaemon implements Daemon {
      private Logger logger;
      public LocalAgent frame;
  
      public void destroy() {
          logger.info("Server was destroyed ....");
      }
  
      public void init(DaemonContext arg0) throws Exception {
          frame = new LocalAgent();
          logger = Logger.getLogger("daemon");
          logger.addAppender(new ConsoleAppender(new PatternLayout("%d{DATE} [%t] %5p - %m%n")));
          logger.setLevel(Level.ALL);
      }
  
      public void start() throws Exception {
          logger.info("Server was started ....");
          frame.listenSocket();
      }
  
      public void stop() throws Exception {
          logger.info("Server was stoped ....");
      }
  }
  </code></pre>
  
  <p>And the script that I use for running the daemon is ;</p>
  
  <pre><code>#!/bin/sh
  
  # Setup variables
  EXEC=/usr/bin/jsvc
  JAVA_HOME=/usr/lib/jvm/java-6-sun
  CLASS_PATH="/usr/share/java/commons-daemon.jar":"/home/mehmet/Desktop/remoteAgent.jar"
  CLASS=example.Main
  USER=foo
  PID=/tmp/example.pid
  LOG_OUT=/tmp/example.out
  LOG_ERR=/tmp/example.err
  
  do_exec()
  {
      $EXEC -home "$JAVA_HOME" -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS
  }
  
  case "$1" in
      start)
          do_exec
              ;;
      stop)
          do_exec "-stop"
              ;;
      restart)
          if [ -f "$PID" ]; then
              do_exec "-stop"
              do_exec
          else
              echo "service not running, will do nothing"
              exit 1
          fi
              ;;
      *)
              echo "usage: daemon {start|stop|restart}" &gt;&amp;2
              exit 3
              ;;
  esac
  </code></pre>
  
  <p>When I run the script I got this error;</p>
  
  <pre><code>java.lang.NoClassDefFoundError: org/apache/log4j/Layout
  at java.lang.Class.getDeclaredMethods0(Native Method)
  at java.lang.Class.privateGetDeclaredMethods(Class.java:2444)
  at java.lang.Class.getMethod0(Class.java:2687)
  at java.lang.Class.getMethod(Class.java:1620)
  at org.apache.commons.daemon.support.DaemonLoader.load(DaemonLoader.java:129)
      Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Layout
  at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
  ... 5 more
      Cannot load daemon
      Service exit with a return value of 3
  </code></pre>
  
  <p>Can you please help me to solve this ?</p>
  
  <p>Thank you all</p>
  
  <p>EDIT :</p>
  
  <p>now I have changed my class path to ;</p>
  
  <p>CLASS_PATH="/home/mehmet/Desktop/apache-log4j-1.2.16":"/home/mehmet/Desktop/remoteAgent.jar"</p>
  
  <p>but now I got an error ;</p>
  
  <p>Cannot find daemon loader org/apache/commons/daemon/support/DaemonLoader Service exit with a return value of 1  </p>