Reputation: 197
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!");
}
}
Upvotes: 1
Views: 1519
Reputation: 171
Ok, so your code looks all good (well, operational), no problems there. I am 99% certain that it's not a matter of code, but rather building/deployment.
I suggest you do the following:
0) If you use Maven and have a servlet-api dependecy, check the servlet-api dependency scope (and do a project update from the maven context menu if you change anything), otherwise make sure to check exports and deployment assembly
1) clean the project (In eclipse Project -> Clean )
2) clean the tomcat work directory and deploy and/or redeploy (or start a completely new instance of Tomcat)
3) if it still doesn't work try opening the war file (or if deployed from eclipse go to Tomcat work dir and navigate to the folder where the Listener should be located) and check it's actually there.
4) if not, see if you can find any problems in the Problems view as to why it's not being built
5) if the problem persists also check what the class output folder is set to as described by @Shailendra and redo steps 1-4.
Upvotes: 0
Reputation: 9102
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
Upvotes: 2