我正在尝试在java中创建一个Daemon类,所以我实现了一个实现守护进程的类,但是当我运行它时,我总是会遇到错误。
我用的那个是;
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 = “/家/穆罕默德/桌面/阿帕奇-的log4j-1.2.16”: “/家/穆罕默德/桌面/ remoteAgent.jar”
但现在我收到了错误;
无法找到守护进程加载器org / apache / commons / daemon / support / DaemonLoader服务出口,返回值为1
答案 0 :(得分:2)
java.lang.NoClassDefFoundError: org/apache/log4j/Layout
以上表示您应该将log4j添加到类路径中(在脚本中使用log4j jar的位置编辑CLASS_PATH
)
答案 1 :(得分:0)