Issue
I am trying to read a java class as soon as I run the project on tomcat, but I get the java class error not found exception
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<listener>
<listener-class>
Bot.CarregarBot
</listener-class>
</listener>
</web-app>
The class I want to load when I run the project is the Bot.CarregarBot class;
When I run the project and the server is started, I get the following error:
GRAVE: Error configuring application listener of class [Bot.CarregarBot]
java.lang.ClassNotFoundException: Bot.CarregarBot
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1365)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:540)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:521)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4607)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5146)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
at java.util.concurrent.FutureTask.run(Unknown Source)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
at java.util.concurrent.FutureTask.run(Unknown Source)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
class CarregarBot.java
package Bot;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class CarregarBot implements ServletContextListener {
String action;
String requestA;
String responseA;
org.alicebot.ab.Bot bot;
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Starting up!");
org.alicebot.ab.MagicStrings.setRootPath();
org.alicebot.ab.AIMLProcessor.extension = new org.alicebot.ab.PCAIMLProcessorExtension();
String botName = "alice2";
action = "chat";
bot = new org.alicebot.ab.Bot(botName, org.alicebot.ab.MagicStrings.root_path, action);
System.out.println("--- Carregou Classe ---");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("Shutting down!");
}
}
Solution
Generally in a typical java web application (war) structure - the compiled class files should end up in WEB-INF/classes
folder ( unless they can be archived as a jar then they can be out in WEB-INF/lib as well). So try changing your project's build path to <your-project>/WebContent/WEB-INF/classes
. You can refer to the below screen shot attached
Here is class I have tried to use and the output on the console when I start my server
package Bot;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* Application Lifecycle Listener implementation class CarregarBot
*
*/
public class CarregarBot implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("CarregarBot initialized");
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
}
Console output
Aug 27, 2019 1:22:47 AM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/TestWebApp] has started
CarregarBot initialized
Aug 27, 2019 1:22:47 AM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/TestWebApp] is completed
Answered By - Shailendra
Answer Checked By - Willingham (JavaFixing Volunteer)