1

我正在尝试在java中创建一个守护进程类,所以我实现了一个实现守护进程的类,但是当我运行它时,我总是会出错。

我使用的类是;

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 ....");
    }
}

我用于运行守护进程的脚本是;

#!/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

当我运行脚本时出现此错误;

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

你能帮我解决这个问题吗?

谢谢你们

编辑 :

现在我已将课程路径更改为;

CLASS_PATH="/home/mehmet/Desktop/apache-log4j-1.2.16":"/home/mehmet/Desktop/remoteAgent.jar"

但现在我得到了一个错误;

找不到守护程序加载程序 org/apache/commons/daemon/support/DaemonLoader 服务退出,返回值为 1

4

2 回答 2

2
java.lang.NoClassDefFoundError: org/apache/log4j/Layout

以上表明您应该将 log4j 添加到您的类路径(CLASS_PATH在脚本中使用您的 log4j jar 的位置进行编辑)

于 2011-10-10T13:54:34.387 回答
0

你想念 log4j.jar。commons.daemon 依赖于它。

于 2011-10-10T13:55:29.923 回答