安排 "Error configuring application listener of class [Schedule]" 的任务问题

Schedule a task problem with "Error configuring application listener of class [Schedule]"

好吧,首先我对这个项目的所有内容都很陌生,据我所知这是一个 REST maven jersey。我想实现一份每日报告,这样当时钟敲响时,无论我选择什么时间,它都会执行代码。现在我尝试了很多不同的方法,但不管我做什么都不行,代码没有执行。所以我正在尝试 servlet 方法,但出现错误:

INFO: Server version name:   Apache Tomcat/10.0.10
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jul 30 2021 09:51:27 UTC
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 10.0.10.0
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jdk-16.0.1
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           16.0.1+9-24
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         F:\Eclipse Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         F:\Tomcat\apache-tomcat-10.0.10
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=F:\Eclipse Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=F:\Tomcat\apache-tomcat-10.0.10
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=F:\Eclipse Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -XX:+ShowCodeDetailsInExceptionMessages
Oct 23, 2021 9:12:24 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [C:\Program Files\Java\jdk-16.0.1\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Users/CrazymanWnT/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_15.0.1.v20201027-0507/jre/bin/server;C:/Users/CrazymanWnT/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_15.0.1.v20201027-0507/jre/bin;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Python39\Scripts\;C:\Python39\;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;E:\Xampp\php;C:\composer;C:\Users\CrazymanWnT\AppData\Local\Programs\Python\Python37;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Program Files\Prio;C:\Users\CrazymanWnT\Desktop\Botlab;C:\Users\CrazymanWnT\AppData\Local\Programs\Python\Python37\Scripts\;C:\Users\CrazymanWnT\AppData\Local\Programs\Python\Python37\;C:\Users\CrazymanWnT\AppData\Local\Programs\Python\Python39\Scripts\;C:\Users\CrazymanWnT\AppData\Local\Programs\Python\Python39\;C:\Program Files\MySQL\MySQL Shell 8.0\bin\;C:\Users\CrazymanWnT\AppData\Local\Microsoft\WindowsApps;C:\Users\CrazymanWnT\AppData\Roaming\Composer\vendor\bin;C:\Users\CrazymanWnT\.dotnet\tools;C:\Users\CrazymanWnT\AppData\Roaming\npm;E:\Programos\Microsoft VS Code\bin;C:\msys64\usr\bin;;C:\Users\CrazymanWnT\Desktop;;.]
Oct 23, 2021 9:12:24 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Oct 23, 2021 9:12:24 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [605] milliseconds
Oct 23, 2021 9:12:24 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Oct 23, 2021 9:12:24 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/10.0.10]
Oct 23, 2021 9:12:26 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Oct 23, 2021 9:12:26 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class [profito.shop.Schedule]
java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1010)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
    at org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2516)
    at org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:872)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1408)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1252)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:539)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:520)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4640)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5179)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:145)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:886)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:145)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:263)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:432)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:927)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:795)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:345)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:476)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContextListener
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1444)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1252)
    ... 40 more

Oct 23, 2021 9:12:26 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)
Oct 23, 2021 9:12:26 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: One or more listeners failed to start. Full details will be found in the appropriate container log file
Oct 23, 2021 9:12:26 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/shop] startup failed due to previous errors
Oct 23, 2021 9:12:26 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesObjectStreamClassCaches
WARNING: When running on Java 9 or later you need to add "--add-opens=java.base/java.io=null" to the JVM command line arguments to enable ObjectStream cache memory leak protection. Alternatively, you can suppress this warning by disabling ObjectStream class cache memory leak protection.
Oct 23, 2021 9:12:26 PM org.apache.catalina.loader.WebappClassLoaderBase checkThreadLocalsForLeaks
WARNING: When running on Java 9 or later you need to add "--add-opens=java.base/java.lang=null" to the JVM command line arguments to enable ThreadLocal memory leak detection. Alternatively, you can suppress this warning by disabling ThreadLocal memory leak detection.
Oct 23, 2021 9:12:26 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesRmiTargets
WARNING: When running on Java 9 or later you need to add "--add-opens=java.rmi/sun.rmi.transport=null" to the JVM command line arguments to enable RMI Target memory leak detection. Alternatively, you can suppress this warning by disabling RMI Target memory leak detection.
Oct 23, 2021 9:12:26 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Oct 23, 2021 9:12:26 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [1959] milliseconds

我的web.xml:

<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>profito.shop</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/v1/*</url-pattern>
  </servlet-mapping>
<listener>
    <listener-class>
        profito.shop.Schedule
    </listener-class>
</listener>
</web-app>

我的代码:


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class Schedule implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent arg0) {
    System.out.println("Listener is off");
}

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    System.out.println("Listener initialized.");

    JobDetail job = JobBuilder.newJob(DailyRepot.class)
            .withIdentity("dummyJobName", "group1").build();
    Trigger trigger = TriggerBuilder
            .newTrigger()
            .withIdentity("dummyTriggerName", "group1")
            .forJob(job)
            .withSchedule(

    CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();

            //schedule it
    Scheduler scheduler;
        try {
            scheduler = new StdSchedulerFactory().getScheduler();               
            scheduler.start();
            scheduler.scheduleJob(job, trigger);

        } catch (SchedulerException e) {                    
            e.printStackTrace();            
        }

}

class DailyRepot implements Job
{

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("Hello Quartz!");            
    }   
}}

我知道问题可能比我现在知道的还要多,我很高兴你们中的任何人都可以与我分享它们,如果可能甚至分享一些解决方案。谢谢!

WebappClassLoaderBase 在默认包

中找不到 class Schedule

我建议你将 Schedule class 放入一个包中,如果它还没有的话,并使用 class 的完全限定名称作为听众 class 喜欢这样

<listener>
    <listener-class>your.package.Schedule</listener-class>
</listener>

并注释掉或删除这两个定义,因为 Schedule 不是 Servlet

<servlet>
    <servlet-name>ScheduleServlet</servlet-name>
    <servlet-class>Schedule</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ScheduleServlet</servlet-name>
    <url-pattern>/ScheduleServlet</url-pattern>
</servlet-mapping>

您正在使用 Tomcat 10,它使用 Servlet API 版本 5.0

在 Servlet API 版本 5.0 中,包 javax.servlet 已重命名为 jakarta.servlet

如果您想继续使用 Tomcat 10,您应该将其添加到您的 pom.xml

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>5.0.0</version>
    <scope>provided</scope>
</dependency>

并将导入语句更新为 jakarta.servlet。

否则只使用Tomcat 9 不改变任何东西