title: Embedded Jetty H2 JNDI Datasouce - javax.naming.NameNotFoundException; remaining name
body: <p>I'm trying to setup a JNDI datasource for H2 database using Jetty Web Server; I have un-commented plus config in start.ini of Jetty and also added Configurations to jetty.xml as per Jetty documentation and created a jetty-web.xml which configures datasource to an existing WebAppContext. When I try to get datasource using intialcontext.lookup() all I get is a javax.naming.NameNotFoundException; remaining name </p>
  
  <pre><code>javax.naming.NameNotFoundException; remaining name 'env/jdbc/MyH2DS'
      at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:505)
      at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:536)
      at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:551)
      at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:117)
      at javax.naming.InitialContext.lookup(InitialContext.java:411)
      at com.tutorial.DbConnector.&lt;init&gt;(DbConnector.java:25)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
      at java.lang.Class.newInstance0(Class.java:374)
      at java.lang.Class.newInstance(Class.java:327)
      at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
      at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
      at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
      at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
      at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
      at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
      at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
      at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
      at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:99)
      at com.sun.el.parser.AstValue.getValue(AstValue.java:158)
      at com.sun.el.parser.AstDeferredExpression.getValue(AstDeferredExpression.java:59)
      at com.sun.el.parser.AstCompositeExpression.getValue(AstCompositeExpression.java:64)
      at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
      at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
      at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
      at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
      at com.sun.faces.renderkit.html_basic.LabelRenderer.encodeBegin(LabelRenderer.java:121)
      at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1755)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
      at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
      at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:681)
      at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:452)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:138)
      at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:564)
      at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:213)
      at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1083)
      at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:379)
      at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175)
      at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1017)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
      at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
      at org.eclipse.jetty.server.Server.handle(Server.java:445)
      at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:260)
      at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:225)
      at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
      at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527)
      at java.lang.Thread.run(Thread.java:722)
  Jun 05, 2013 4:23:47 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
  SEVERE: Error Rendering View[/welcome.xhtml]
  javax.el.ELException: /welcome.xhtml @13,86 value="Selected From DataSource: #{dbConnector.dsConnection}": java.lang.NullPointerException
      at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
      at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
      at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
      at com.sun.faces.renderkit.html_basic.LabelRenderer.encodeBegin(LabelRenderer.java:121)
      at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1755)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
      at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
      at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:681)
      at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:452)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:138)
      at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:564)
      at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:213)
      at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1083)
      at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:379)
      at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175)
      at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1017)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
      at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
      at org.eclipse.jetty.server.Server.handle(Server.java:445)
      at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:260)
      at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:225)
      at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
      at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: java.lang.NullPointerException
      at com.tutorial.DbConnector.getDsConnection(DbConnector.java:40)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
      at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
      at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
      at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
      at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
      at com.sun.el.parser.AstDeferredExpression.getValue(AstDeferredExpression.java:59)
      at com.sun.el.parser.AstCompositeExpression.getValue(AstCompositeExpression.java:64)
      at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
      at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
      ... 34 more
  
  2013-06-05 16:23:47.496:WARN:oejs.ServletHandler:qtp24549422-11: 
  javax.servlet.ServletException
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
      at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:681)
      at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:452)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:138)
      at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:564)
      at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:213)
      at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1083)
      at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:379)
      at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175)
      at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1017)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
      at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
      at org.eclipse.jetty.server.Server.handle(Server.java:445)
      at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:260)
      at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:225)
      at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
      at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: 
  java.lang.NullPointerException
      at com.tutorial.DbConnector.getDsConnection(DbConnector.java:40)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
      at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
      at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
      at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
      at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
      at com.sun.el.parser.AstDeferredExpression.getValue(AstDeferredExpression.java:59)
      at com.sun.el.parser.AstCompositeExpression.getValue(AstCompositeExpression.java:64)
      at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
      at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
      at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
      at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
      at com.sun.faces.renderkit.html_basic.LabelRenderer.encodeBegin(LabelRenderer.java:121)
      at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1755)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
      at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
      at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:681)
      at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:452)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:138)
      at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:564)
      at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:213)
      at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1083)
      at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:379)
      at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175)
      at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1017)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
      at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
      at org.eclipse.jetty.server.Server.handle(Server.java:445)
      at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:260)
      at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:225)
      at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
      at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p><strong>jetty-web.xml :</strong></p>
  
  <pre><code>&lt;Configure class="org.eclipse.jetty.webapp.WebAppContext" id="wac"&gt;
          &lt;Arg&gt;jdbc/MyH2DS&lt;/Arg&gt;
          &lt;Arg&gt;
              &lt;New class="org.h2.jdbcx.JdbcDataSource"&gt;
                  &lt;Set name="URL"&gt;jdbc:h2:/test&lt;/Set&gt;
                  &lt;Set name="user"&gt;sa&lt;/Set&gt;
                  &lt;Set name="password"&gt;&lt;/Set&gt;
              &lt;/New&gt;
          &lt;/Arg&gt;
  &lt;/Configure&gt;
  </code></pre>
  
  <p><strong>Server code :</strong> </p>
  
  <pre><code>Server server = new Server(8080);
          WebAppContext wac = new WebAppContext();
          File warPath = new File("C:/Users/XCB638.DS/workspace/MyJSFProject/WebContent/WEB-INF");
          wac.setWar(warPath.getAbsolutePath());
          wac.setContextPath("/");
          wac.setBaseResource(new ResourceCollection(new String[] { "./WebContent", "build/classes" }));
          server.setHandler(wac);
          try
          {
              server.start();
              server.join();
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
  </code></pre>