title: How to lookup a local bean in wildfly with jndi
body: <p>I have what appears to be the same question as <a href="https://stackoverflow.com/questions/26018100/wildfly-jndi-lookup-for-local-ejb-deployed-in-a-war" title="WildFly JNDI lookup for local EJB deployed in a war">WildFly JNDI lookup for local EJB deployed in a war</a>, except that I can't get his solution to work.</p>
  
  <p>I have an ear with EJBs deployed in the jar jaws-server-ejb3.  Inside my app I want to call a method on a bean.  I've tried this with and without <code>@LocalBean</code> on my bean.</p>
  
  <p>When wildfly (8.2) starts it prints out:</p>
  
  <pre><code>java:global/jaws/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
  java:app/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
  java:module/ForecastService!com.termalabs.common.service.ForecastServiceRemote
  java:jboss/exported/jaws/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
  java:global/jaws/jaws-server-ejb3/ForecastService
  java:app/jaws-server-ejb3/ForecastService
  java:module/ForecastService
  </code></pre>
  
  <p>When I add @LocalBean to my bean I see:</p>
  
  <pre><code>java:global/jaws/jaws-server-ejb3/ForecastService!com.termalabs.server.service.ForecastService
  java:app/jaws-server-ejb3/ForecastService!com.termalabs.server.service.ForecastService
  java:module/ForecastService!com.termalabs.server.service.ForecastService
  java:global/jaws/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
  java:app/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
  java:module/ForecastService!com.termalabs.common.service.ForecastServiceRemote
  java:jboss/exported/jaws/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
  </code></pre>
  
  <p>So it seems clear from all I read that I should be able to lookup this bean with "java:app/jaws-server-ejb3/ForecastService" or "java:app/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote"</p>
  
  <p>Here is my lookup which I have hard coded at the moment:</p>
  
  <pre><code>public static Object getBeanInternal(String beanName, Class remoteClass) throws NamingException {
      System.out.println("Getting internal bean: " + beanName + " for class "  + remoteClass.getName());
      final Hashtable jndiProperties = new Hashtable();
      jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
      InitialContext ctx = new InitialContext(jndiProperties);
      Object ejb = ctx.lookup("java:app/jaws-server-ejb3/ForecastService");
  //    Object ejb = ctx.lookup("java:global/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastService");
      return ejb;
  }
  </code></pre>
  
  <p>I have tried this with an empty jndiProperties table, or with the setting quoted above.  No matter what I've tried I end up with a NamingException like</p>
  
  <pre><code>2015-04-07 15:50:01,836 WARN  [com.termalabs.server.system.AutosysJammerSequence] (JammerManager-2015/04/07 15:49:46.710 MDT-0) Can't lookup: javax.naming.NameNotFoundException: java:app/jaws-server-ejb3/ForecastService
      at org.jboss.as.naming.InitialContext$DefaultInitialContext.findContext(InitialContext.java:187)
      at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:231)
      at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:188)
      at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)
      at javax.naming.InitialContext.lookup(InitialContext.java:417) [rt.jar:1.8.0_25]
      at javax.naming.InitialContext.lookup(InitialContext.java:417) [rt.jar:1.8.0_25]
      at com.termalabs.common.ejb3.EJB3Util.getBeanInternal(EJB3Util.java:55) [jaws-common-ejb3intf-1.0.jar:]
      at com.termalabs.server.system.AutosysJammerSequence.runJammerSequence(AutosysJammerSequence.java:427) [jaws-server-base-5.1dev.jar:]
      at com.termalabs.server.system.JammerSequence.run(JammerSequence.java:125) [jaws-server-base-5.1dev.jar:]
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [rt.jar:1.8.0_25]
      at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_25]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_25]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_25]
      at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_25]
  </code></pre>
  
  <p>I can get to this bean using the remote interface, but that doesn't seem right since I am running this code from the same ear.  I shouldn't need to do that.</p>
  
  <p>What am I missing?</p>
  
  <p>Thanks,</p>
  
  <p>Michael</p>