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.
The class that I use for that is ;
public class RemoteAgentDaemon imp开发者_StackOverflow中文版lements 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 ....");
}
}
And the script that I use for running the daemon is ;
#!/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}" >&2
exit 3
;;
esac
When I run the script I got this error;
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
Can you please help me to solve this ?
Thank you all
EDIT :
now I have changed my class path to ;
CLASS_PATH="/home/mehmet/Desktop/apache-log4j-1.2.16":"/home/mehmet/Desktop/remoteAgent.jar"
but now I got an error ;
Cannot find daemon loader org/apache/commons/daemon/support/DaemonLoader Service exit with a return value of 1
java.lang.NoClassDefFoundError: org/apache/log4j/Layout
The above indicates that you should add log4j to your classpath (edit CLASS_PATH
in the script with the location of your log4j jar)
you miss log4j.jar. commons.daemon has a dependency on it.
精彩评论