title: JSF 2.3 - Servlet 4 - JavaEE 8 and Tomcat9: Unable to find CDI BeanManager
body: <p>I am trying to migrate from JSF 2.1 to 2.3/Servlet 4/ Tomcat 9 but I have some trouble whith CDI configuration.
  I followed <a href="http://balusc.omnifaces.org/2013/10/how-to-install-cdi-in-tomcat.html" rel="nofollow noreferrer">this guide</a> from BalusC to enable CDI on Tomcat 9 (in my case) but I still get this exception when starting my app: </p>
  
  <pre><code>com.sun.faces.config.ConfigurationException: Factory 'javax.faces.lifecycle.ClientWindowFactory' was not configured properly.
      at com.sun.faces.config.processor.FactoryConfigProcessor.verifyFactoriesExist(FactoryConfigProcessor.java:357)
      at com.sun.faces.config.processor.FactoryConfigProcessor.process(FactoryConfigProcessor.java:243)
      at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:443)
      at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:237)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4641)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5109)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:695)
      at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1737)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287)
      at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
      at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
      at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:457)
      at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:406)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287)
      at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
      at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
      at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
      at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
      at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
      at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
      at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
      at sun.rmi.transport.Transport$1.run(Transport.java:200)
      at sun.rmi.transport.Transport$1.run(Transport.java:197)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: javax.faces.FacesException: Unable to find CDI BeanManager
      at com.sun.faces.application.ApplicationImpl.isJsf23(ApplicationImpl.java:2714)
      at com.sun.faces.application.ApplicationImpl.addELResolver(ApplicationImpl.java:549)
      at javax.faces.application.ApplicationWrapper.addELResolver(ApplicationWrapper.java:611)
      at javax.faces.application.ApplicationWrapper.addELResolver(ApplicationWrapper.java:611)
      at org.jboss.weld.environment.servlet.jsf.WeldApplication.&lt;init&gt;(WeldApplication.java:72)
      at org.jboss.weld.environment.servlet.jsf.WeldApplicationFactory.getApplication(WeldApplicationFactory.java:46)
      at com.sun.faces.application.InjectionApplicationFactory.getApplication(InjectionApplicationFactory.java:88)
      at com.sun.faces.config.InitFacesContext.getApplication(InitFacesContext.java:153)
      at com.sun.faces.lifecycle.ClientWindowFactoryImpl.&lt;init&gt;(ClientWindowFactoryImpl.java:63)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at java.lang.Class.newInstance(Class.java:442)
      at javax.faces.FactoryFinderInstance.getImplGivenPreviousImpl(FactoryFinderInstance.java:391)
      at javax.faces.FactoryFinderInstance.getImplementationInstance(FactoryFinderInstance.java:255)
      at javax.faces.FactoryFinderInstance.getFactory(FactoryFinderInstance.java:529)
      at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:292)
      at com.sun.faces.config.processor.FactoryConfigProcessor.verifyFactoriesExist(FactoryConfigProcessor.java:354)
      ... 48 more
  </code></pre>
  
  <p>Note that before following the guide, I had the same exception but it was thrown from another class (<code>com.sun.faces.el.ELUtils.java</code> method <code>tryAddCDIELResolver(FacesCompositeELResolver composite)</code>) and after the guide it's thrown from <code>com.sun.faces.application.ApplicationImpl.java</code> method <code>private boolean isJsf23()</code>.
  My librairies version are:</p>
  
  <pre><code>    &lt;icefaces-ace.version&gt;3.2.0&lt;/icefaces-ace.version&gt;
      &lt;javax.jms-api.version&gt;2.0.1&lt;/javax.jms-api.version&gt;
      &lt;el-api.version&gt;2.2&lt;/el-api.version&gt;
      &lt;javax.el-api.version&gt;3.0.0&lt;/javax.el-api.version&gt;
      &lt;jstl.version&gt;1.2&lt;/jstl.version&gt;
      &lt;javax.faces-api.version&gt;2.3&lt;/javax.faces-api.version&gt;&lt;!--JSF API--&gt;
      &lt;javax.faces.version&gt;2.3.8&lt;/javax.faces.version&gt; &lt;!--Mojarra--&gt;
      &lt;javax.ws.rs-api.version&gt;2.1.1&lt;/javax.ws.rs-api.version&gt;
      &lt;javax.servlet-api.version&gt;4.0.1&lt;/javax.servlet-api.version&gt;
      &lt;javax.servlet.jsp.jstl-api.version&gt;1.2.2&lt;/javax.servlet.jsp.jstl-api.version&gt;
      &lt;javaee-web-api.version&gt;8.0&lt;/javaee-web-api.version&gt;
      &lt;weld-servlet-shaded.version&gt;3.0.5.Final&lt;/weld-servlet-shaded.version&gt;
  </code></pre>
  
  <p>Could someone tell me what am I doing wrong?
  Thanks a lot.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The piece of code which trying to find the <code>BeanManager</code> is this one:</p>
  
  <pre><code> public static BeanManager getCdiBeanManager(FacesContext facesContext) {
      BeanManager result = null;
  
      if (facesContext != null &amp;&amp; facesContext.getAttributes().containsKey(RIConstants.CDI_BEAN_MANAGER)) {
          result = (BeanManager) facesContext.getAttributes().get(RIConstants.CDI_BEAN_MANAGER);
      } else if (facesContext != null &amp;&amp; facesContext.getExternalContext().getApplicationMap().containsKey(RIConstants.CDI_BEAN_MANAGER)) {
          result = (BeanManager) facesContext.getExternalContext().getApplicationMap().get(RIConstants.CDI_BEAN_MANAGER);
      } else {
          try {
              InitialContext initialContext = new InitialContext();
              result = (BeanManager) initialContext.lookup("java:comp/BeanManager");
          }
          catch (NamingException ne) {
              try {
                  InitialContext initialContext = new InitialContext();
                  result = (BeanManager) initialContext.lookup("java:comp/env/BeanManager");
              }
              catch (NamingException ne2) {
              }
          }
  
          if (result == null &amp;&amp; facesContext != null) {
              Map&lt;String, Object&gt; applicationMap = facesContext.getExternalContext().getApplicationMap();
              result = (BeanManager) applicationMap.get("org.jboss.weld.environment.servlet.javax.enterprise.inject.spi.BeanManager");
          }
  
          if (result != null &amp;&amp; facesContext != null) {
              facesContext.getAttributes().put(RIConstants.CDI_BEAN_MANAGER, result);
              facesContext.getExternalContext().getApplicationMap().put(RIConstants.CDI_BEAN_MANAGER, result);
          }
      }
  
      return result;
  }
  </code></pre>
  
  <p>I added some breakpoints to check the <code>result' and it is really</code>null<code>but the</code>facesContext` isn't.</p>