Alright so first of all I'm new to everything on this project which is a REST maven jersey one as far as I know. I want to implement a daily report so when the clock strikes whichever time I choose it executes a code. Now I tried a lot of different ways and it isn't going well whatever i do doesn't work, the code doesn't get executed. So I'm trying the servlet aproach but I'm getting an error:
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
My 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>
My code:
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!");
}
}}
I know there's probably more problems than I know of now and I'm glad if anyone of you can share them with me and if possible even share some solutions. Thank you!
CodePudding user response:
WebappClassLoaderBase is not finding the class Schedule in the default package
I suggest you to put the Schedule class in a package if it isn't already and use the fully qualified name of the class as the listener class like this
<listener>
<listener-class>your.package.Schedule</listener-class>
</listener>
And comment-out or delete these two definitions because Schedule is not a 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>
You are using Tomcat 10 which uses Servlet API Version 5.0
In Servlet API Version 5.0 the package javax.servlet has been renamed to jakarta.servlet
If you want to keep using Tomcat 10 you should add this to your pom.xml
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
And update the import statements to jakarta.servlet.
Otherwise just use Tomcat 9 without changing anything