ClassNotFoundException:org.slf4j.LoggerFactory Maven

时间:2014-11-27 01:41:40

标签: java eclipse maven tomcat

我正在尝试在服务器上运行带有tomcat的.war文件。每次我启动tomcat时都应该设置一个计时器,它在一周内运行一次.war文件的init()方法。我是用Quartz的CronTrigger做的。 JobBuilder调用HelloJob类,其中(运行时)创建了一个巨大的excel表。

public class CronTrigger implements Servlet

{    
    public void init(ServletConfig arg0) throws ServletException {

        JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("Batch Validation Trigger", "group1").build();

        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("Batch Validation Trigger", "group1").withSchedule(CronScheduleBuilder.cronSchedule("0 45 14 ? * TUE")).build();
        Logger root = (Logger)LoggerFactory.getLogger(CronTrigger.class);
        root.isDebugEnabled();
        //schedule it
        Scheduler scheduler = null;
        try {
            scheduler = new StdSchedulerFactory().getScheduler();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        try {
            scheduler.start();
        } catch (SchedulerException e1) {
            e1.printStackTrace();
        }
        try {
            scheduler.scheduleJob(job, trigger);
        } catch (SchedulerException e2) {
            e2.printStackTrace();
        }
    }
}    

我的web.xml:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>WOQC</display-name>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
      <servlet>
    <servlet-name>CronTrigger</servlet-name>
    <servlet-class>timer.CronTrigger</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CronTrigger</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

当我尝试运行它时,它会返回一个错误和一个异常: NoClassDefFoundError的:

SEVERE: StandardWrapper.Throwable
    java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at timer.CronTrigger.init(CronTrigger.java:48)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5231)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5518)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
        ... 13 more

ClassNotFoundException的:

SEVERE: Servlet /WO threw load() exception
java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at timer.CronTrigger.init(CronTrigger.java:48)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5231)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5518)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

我已经阅读了有关此异常的问题,并将slf4j-nop-1.7.7.jar和slf4j-api-1.7.7.jar添加到了类路径中。我知道问题属于LoggerFactory的实现,但我找不到一个很好的例子来解决我的问题。如果您有任何想法,请回复。

提前致谢!

2 个答案:

答案 0 :(得分:1)

ClassNotFoundException提示IDE本身未识别第三方库。您应该将jar文件(第三方库)添加到 WEB-INF / lib 文件夹中。现在它应该正常运作。

答案 1 :(得分:0)

在lib目录中添加你的slf4j.jar。

战争文件结构如下

WebContent
    --- WEB-INF
         ----web.xml (Deployment Descriptor)
         ---- lib 
                --- Add your libraries here i.e. slf4j*.jar and others