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: [Root exception is java.lang.ClassNotFoundException:]

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.*;

public class BootstrapTheWebServer implements ServletContextListener {

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

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

Try adding below dependency in your class path:


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

Related post

Copyright (C), All Rights Reserved.

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