Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add context.xml file to embedded tomcat server

I'm trying to run my application in an embedded tomcat server using Spring-boot. I'm having my JNDI Resources and Environments configured in an XML file, placed under src/main/webapp/META-INF/context.xml in my application. When I deploy this application in a standard servlet container it works. But when I start it using an embedded server it does not recognize the JNDI resources that are present in the context files.

I tried adding the context file to the embeddedTomcatContainer programatically by using the following code. Referred from this blog -

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            tomcat.enableNaming();

            Context context = new StandardContext();
            File f = new File("D:\\Workspace\\SpringBoot\\src\\main\\webapp\\META-INF\\context.xml");


            try {
                context.setConfigFile(f.toURI().toURL());
                tomcat.getHost().addChild(context);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

But I'm getting the following Exception. Can someone please let me know how to add context.xml files to embedded tomcat server ?

org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@100862e: startup date [Mon Nov 23 17:21:38 IST 2015]; root of context hierarchy
2015-11-23 17:21:39.811  INFO 69992 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-11-23 17:21:39.822  INFO 69992 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-11-23 17:21:39.822  INFO 69992 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.28
2015-11-23 17:21:39.842 ERROR 69992 --- [cat-startStop-1] org.apache.catalina.core.ContainerBase   : A child container failed during start

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to initialize component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[null]]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122) [na:1.7.0_85]
    at java.util.concurrent.FutureTask.get(FutureTask.java:188) [na:1.7.0_85]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_85]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_85]
Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[null]]
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:106) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139) [tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 6 common frames omitted
Caused by: java.lang.NullPointerException: null
    at org.apache.catalina.core.StandardContext.getObjectKeyPropertiesNameOnly(StandardContext.java:6247) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardContext.getObjectNameKeyProperties(StandardContext.java:6228) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleMBeanBase.initInternal(LifecycleMBeanBase.java:61) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase.initInternal(ContainerBase.java:882) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardContext.initInternal(StandardContext.java:6257) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102) [tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 7 common frames omitted

2015-11-23 17:21:39.982  INFO 69992 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-11-23 17:21:39.982  INFO 69992 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1413 ms
2015-11-23 17:21:40.122  INFO 69992 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-11-23 17:21:40.122  INFO 69992 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-11-23 17:21:40.152 ERROR 69992 --- [           main] org.apache.catalina.core.ContainerBase   : A child container failed during start

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[na:1.7.0_85]
    at java.util.concurrent.FutureTask.get(FutureTask.java:188) ~[na:1.7.0_85]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:439) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:769) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:337) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:89) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.(TomcatEmbeddedServletContainer.java:76) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:452) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at com.ln.risk.boot.LnBootApplication$1.getTomcatEmbeddedServletContainer(LnBootApplication.java:74) [classes/:na]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:167) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) [spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at com.ln.risk.boot.LnBootApplication.main(LnBootApplication.java:39) [classes/:na]
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) ~[na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_85]
    at java.lang.Thread.run(Thread.java:745) ~[na:1.7.0_85]
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:924) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 6 common frames omitted

2015-11-23 17:21:40.152  WARN 69992 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
2015-11-23 17:21:40.152  INFO 69992 --- [           main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/D:/Workspaces/SpringBoot/ln-boot/target/classes/, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-starter-web/1.3.0.RELEASE/spring-boot-starter-web-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-starter/1.3.0.RELEASE/spring-boot-starter-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot/1.3.0.RELEASE/spring-boot-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.3.0.RELEASE/spring-boot-autoconfigure-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-starter-logging/1.3.0.RELEASE/spring-boot-starter-logging-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar, file:/C:/Users/221008/.m2/repository/ch/qos/logback/logback-core/1.1.3/logback-core-1.1.3.jar, file:/C:/Users/221008/.m2/repository/org/slf4j/slf4j-api/1.7.13/slf4j-api-1.7.13.jar, file:/C:/Users/221008/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.13/jcl-over-slf4j-1.7.13.jar, file:/C:/Users/221008/.m2/repository/org/slf4j/jul-to-slf4j/1.7.13/jul-to-slf4j-1.7.13.jar, file:/C:/Users/221008/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.13/log4j-over-slf4j-1.7.13.jar, file:/C:/Users/221008/.m2/repository/org/yaml/snakeyaml/1.16/snakeyaml-1.16.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-starter-validation/1.3.0.RELEASE/spring-boot-starter-validation-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/hibernate/hibernate-validator/5.2.2.Final/hibernate-validator-5.2.2.Final.jar, file:/C:/Users/221008/.m2/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar, file:/C:/Users/221008/.m2/repository/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar, file:/C:/Users/221008/.m2/repository/com/fasterxml/classmate/1.1.0/classmate-1.1.0.jar, file:/C:/Users/221008/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.6.3/jackson-databind-2.6.3.jar, file:/C:/Users/221008/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.6.3/jackson-annotations-2.6.3.jar, file:/C:/Users/221008/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.6.3/jackson-core-2.6.3.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-web/4.2.3.RELEASE/spring-web-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-aop/4.2.3.RELEASE/spring-aop-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-beans/4.2.3.RELEASE/spring-beans-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-context/4.2.3.RELEASE/spring-context-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-webmvc/4.2.3.RELEASE/spring-webmvc-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-expression/4.2.3.RELEASE/spring-expression-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/1.3.0.RELEASE/spring-boot-starter-tomcat-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.0.28/tomcat-embed-core-8.0.28.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/8.0.28/tomcat-embed-el-8.0.28.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/embed/tomcat-embed-logging-juli/8.0.28/tomcat-embed-logging-juli-8.0.28.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/8.0.28/tomcat-embed-websocket-8.0.28.jar, file:/C:/Users/221008/.m2/repository/mysql/mysql-connector-java/5.1.37/mysql-connector-java-5.1.37.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/tomcat-dbcp/8.0.28/tomcat-dbcp-8.0.28.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/tomcat-juli/8.0.28/tomcat-juli-8.0.28.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-core/4.2.3.RELEASE/spring-core-4.2.3.RELEASE.jar]
2015-11-23 17:21:40.152 ERROR 69992 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at com.ln.risk.boot.LnBootApplication.main(LnBootApplication.java:39) [classes/:na]
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:99) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.(TomcatEmbeddedServletContainer.java:76) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:452) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at com.ln.risk.boot.LnBootApplication$1.getTomcatEmbeddedServletContainer(LnBootApplication.java:74) ~[classes/:na]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:167) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    ... 8 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardServer[-1]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:337) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:89) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    ... 14 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Tomcat]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:769) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 16 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:439) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 18 common frames omitted
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:924) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 20 common frames omitted

EDIT:

I also tried some more tomcat APIs to load the context.xml file (which has several JNDI Resources and Environments defined) to the embedded server instance, but I keep getting a Null Pointer Exception. PFB the different APIs that I tried to use.

1.  tomcat.addContext("/my-boot", System.getProperty("user.dir")+ "/src/main/webapp/META-INF/context.xml");

2.  StandardContext context = new StandardContext();
context.setDefaultContextXml(System.getProperty("user.dir") + "/src/main/webapp/META-INF/context.xml");
tomcat.getHost().addChild(context);

For both the approaches I'm getting a NullPointerException while tomcat is trying to get the name of my resource. By the name of the API methods, I feel that there should be a way to get this done. Please assist with this.

2015-11-25 11:35:01.441  INFO 74428 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.28
2015-11-25 11:35:01.471  WARN 74428 --- [ost-startStop-1] o.a.catalina.deploy.NamingResourcesImpl  : Failed to create MBean for naming resource [null]

java.lang.NullPointerException: null
    at javax.management.ObjectName.quote(ObjectName.java:1833) ~[na:1.7.0_85]
    at org.apache.catalina.mbeans.MBeanUtils.createObjectName(MBeanUtils.java:371) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.mbeans.MBeanUtils.createMBean(MBeanUtils.java:159) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.deploy.NamingResourcesImpl.initInternal(NamingResourcesImpl.java:939) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardContext.initInternal(StandardContext.java:6261) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_85]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_85]
like image 388
SanjaySSN Avatar asked Nov 23 '15 12:11

SanjaySSN


People also ask

Where do I put the context path in server xml?

xml. The second option is to set the context path of the application in the server. xml (which is located at $CATALINA_HOME\conf). Note: defining the context path manually has the side effect that the application is deployed twice by default: at http://localhost:8080/ExampleApp/ as well as at http://localhost:8080/.

Where is context xml in Tomcat?

Individual XML files located in $CATALINA_HOME/conf/[enginename]/[hostname]/ The name of each individual XML file will define a new context path.

Where is context path in Tomcat?

Tomcat supports nested context paths. These are embedded in the WAR filename after a single hash character. For example, if you deploy a WAR file called demo#v1. war , it will be made available under the demo/v1 context.


Video Answer


1 Answers

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            tomcat.enableNaming();

            Context context = new StandardContext();
                File file = new File("D:\\Workspace\\SpringBoot\\src\\main\\webapp\\META-INF\\context.xml");
    System.out.println("File Location: "+file.exists());
    //First check for file path is true or not. "file.exists()" method will check the location and return true or flase


            try {
                context.setConfigFile(f.toURI().toURL());
                tomcat.getHost().addChild(context);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }
like image 79
CannonSuyash Avatar answered Oct 23 '22 16:10

CannonSuyash