Servlet: Cannot instantiate javaURLContextFactory only if load-on-startup

In Tomcat 7 (JDK 1.7) I have a servlet that in load on startup returns this error:

javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.naming.java.javaURLContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.naming.java.javaURLContextFactory]

but if I call same servlet from browser it runs.

-------------Problems Reply------------

The error occurs on the servlet init() method that is invoked on start up only. Seems you are missing one or more JAR files from the classpath

Tomcat 6 and 7 support servlet 2.5 standard. The standard includes a better way to do web-server-bootstrap actions, through the ServletContextListener interface.

Tomcat 7 supports servlet 3.0 standard, where you can annotate your class to avoid web.xml configuratiuon

import javax.servlet.*;
import javax.servlet.annotation.*;

@WebListener
public class BootstrapTheWebServer implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent event) {
// invoked on web server start up
// event.getServletContext() to get servlet context
}

@Override
public void contextDestroyed(ServletContextEvent event) {
// invoked on web server shutdown
}
}

Try adding below dependency in your class path:

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.0.23</version>
</dependency>

Category:java Views:3 Time:2018-09-11

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.122 (s). 11 q(s)