title: Migrating an EJB local client from JBoss 6 to Wildfly 10
body: <p>I am facing the following JNDI lookup problem when getting a reference to a session bean in a legacy application during the <strong>migration from JBoss 6 to WildFly 10</strong>.</p>
  
  <p>The following code, which works in JBoss 6, gets a reference to the session bean "OMGenEJB", which is then used to obtain a proxy object "genhome" to interact with the bean.</p>
  
  <pre><code>    try {
          InitialContext ic = new InitialContext();
          Object objRef = ic.lookup("ejb/OMGenEJB");
          OMGenEJBHome genhome = 
            (OMGenEJBHome) PortableRemoteObject.narrow ( objRef, OMGenEJBHome.class );
  </code></pre>
  
  <p>...</p>
  
  <p>The <strong>EJB home</strong> interface is <strong>OMGenEJBHome</strong>.</p>
  
  <p>The EJB implementation class is OMGenBean.</p>
  
  <p>The bean is defined in the "ejb-jar.xml", as follows:</p>
  
  <pre><code>  &lt;session &gt;
       &lt;ejb-name&gt;OMGenEJB&lt;/ejb-name&gt;
       &lt;home&gt;com.irisel.oms.ejb.interfaces.OMGenEJBHome&lt;/home&gt;
       &lt;remote&gt;com.irisel.oms.ejb.interfaces.OMGenEJB&lt;/remote&gt;
       &lt;service-endpoint&gt;com.irisel.oms.ws.OMBrowserEndpoint&lt;/service-endpoint&gt;
       &lt;ejb-class&gt;com.irisel.oms.ejb.browser.OMGenBean&lt;/ejb-class&gt;
       &lt;session-type&gt;Stateless&lt;/session-type&gt;
       &lt;transaction-type&gt;Bean&lt;/transaction-type&gt;
    &lt;/session&gt;
  </code></pre>
  
  <p>Now the client class belongs to a WAR that is in the same EAR as the EJB jar.</p>
  
  <p>The EAR is deployed correctly, and the server at startup logs the following <strong>JNDI entries</strong>:</p>
  
  <pre><code>21:39:18,770 INFO  [org.jboss.as.ejb3.deployment] (MSC service thread 1-3) WFLYEJB0473: JNDI bindings for session bean named 'OMGenEJB' in deployment unit 'subdeployment "HolmesEJB-2.0.jar" of deployment "HolmesBSS-2.0.ear"' are as follows:
  
  java:global/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJB
  java:app/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJB
  java:module/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJB
  java:jboss/exported/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJB
  java:global/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.browser.OMGenBean
  java:app/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.browser.OMGenBean
  java:module/OMGenEJB!com.irisel.oms.ejb.browser.OMGenBean
  java:global/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome
  java:app/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome
  java:module/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome
  java:jboss/exported/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome
  </code></pre>
  
  <p>Below is a modification to print JNDI entries, as seen in the client, both in the initial context, and "java:comp/env"</p>
  
  <pre><code>protected void init2(Credentials creds) throws SQLException, ConfigException, RemoteException {
  
      logger = org.apache.log4j.Logger.getLogger(this.getClass());
  
      try {
  
          logger.info("Connecting to ejb/OMGenEJB");
  
          InitialContext ic = new InitialContext();
  
          // this code lists the entries in the initial context
          NamingEnumeration&lt;NameClassPair&gt; list = ic.list("");
          while (list.hasMore()) {
            System.out.println(list.next().getName());
          }
  
  
          Context envCtx = (Context)ic.lookup("java:comp/env");
  
          // this code lists the entries in java:comp/env context
          list = envCtx.list("");
          while (list.hasMore()) {
                System.out.println(list.next().getName());
          }
  
          Object objRef2 = envCtx.lookup("ejb/OMGenEJB");
  
          logger.info("Narrowing to class OMGenEJBHome #"+OMGenEJBHome.class.hashCode());
          OMGenEJBHome genhome = (OMGenEJBHome)PortableRemoteObject.narrow(objRef2, OMGenEJBHome.class);
  </code></pre>
  
  <p>...</p>
  
  <p>The modified code produces these logs:</p>
  
  <pre><code>23:23:43,445 INFO  [com.irisel.oms.olapi.CLEJBBrowser] (default task-16) Connecting to ejb/OMGenEJB
  23:23:44,274 INFO  [stdout] (default task-16) Mail
  
  23:23:44,669 INFO  [stdout] (default task-16) TransactionManager
  </code></pre>
  
  <p>It turns out that <strong>the initial context is "java:"</strong> because Mail and TransactionManager have JNDI names "java:/Mail" and "java:/TransactionManager" respectively. However, I'd expect the relative path to be relative to java:comp/env.</p>
  
  <p>Next to that, we see the entry "ejb":</p>
  
  <pre><code>23:23:46,163 INFO  [stdout] (default task-16) com.irisel.oms.ws.server.KnockKnockImpl
  
  23:23:46,789 INFO  [stdout] (default task-16) ejb
  
  23:23:50,146 INFO  [stdout] (default task-16) com.irisel.oms.ws.server.HolmesImpl
  </code></pre>
  
  <p>My question is</p>
  
  <ol>
  <li>why "java:comp/env" is no longer the default context but "java:",</li>
  <li><p>why the lookup of "ejb/OMGenEJB" fails also in the modify code</p>
  
  <p>23:23:51,680 INFO  [stdout] (default task-16) Exception :WFLYNAM0062: Failed to lookup env/ejb/OMGenEJB</p></li>
  </ol>
  
  <p>If we try to lookup "ejb" instead, the lookup works fine. </p>
  
  <p>So I tried this as well:</p>
  
  <pre><code>    try {
          logger.info("Connecting to ejb/OMGenEJB");
          InitialContext ic = new InitialContext();
          NamingEnumeration&lt;NameClassPair&gt; list = ic.list("");
          while (list.hasMore()) {
            System.out.println("ic entry:"+list.next().getName());
          }
          Context envCtx = (Context)ic.lookup("java:comp/env/ejb");
          list = envCtx.list("");
          while (list.hasMore()) {
                System.out.println("envCtx entry:"+list.next().getName());
          }
          Object objRef2 = envCtx.lookup("OMGenEJB");
          logger.info("Narrowing to class OMGenEJBHome #"+OMGenEJBHome.class.hashCode());
          OMGenEJBHome genhome = (OMGenEJBHome)PortableRemoteObject.narrow(objRef2, OMGenEJBHome.class);
  
          //browser = genhome.create();
          //className = browser.getClassNames();
  
          genejb = genhome.create();
          appvo = (AppVO) AppVO.readObject64(this.genejb.exec("getApp",null,null));
  
      } catch (Exception ex) {
          System.out.println("Exception :"+ ex.getMessage());
          ConfigException ce = new ConfigException("Cannot initialize remote EJB client");
          ce.initCause(ex);
          throw ce;
      } 
  </code></pre>
  
  <p>Which produces these logs:</p>
  
  <pre><code>00:35:52,048 INFO  [com.irisel.oms.olapi.CLEJBBrowser] (default task-2) Connecting to ejb/OMGenEJB
  00:35:52,823 INFO  [stdout] (default task-2) ic entry:Mail
  
  00:35:53,123 INFO  [stdout] (default task-2) ic entry:TransactionManager
  
  00:35:53,992 INFO  [stdout] (default task-2) envCtx entry:OMBrowserEJB
  
  00:35:54,314 INFO  [stdout] (default task-2) envCtx entry:OMGenEJB
  
  00:36:25,649 INFO  [stdout] (default task-2) Exception :WFLYNAM0062: Failed to lookup env/ejb/OMGenEJB
  
  00:36:25,650 ERROR [stderr] (default task-2) com.irisel.util.ConfigException: Cannot initialize remote EJB client
  
  00:36:25,650 ERROR [stderr] (default task-2)    at com.irisel.oms.olapi.CLEJBBrowser.init2(CLEJBBrowser.java:82)
  ...
  00:36:25,671 ERROR [stderr] (default task-2) Caused by: javax.naming.NamingException: WFLYNAM0062: Failed to lookup env/ejb/OMGenEJB [Root exception is java.lang.RuntimeException: javax.naming.NameNotFoundException: ejb/OMGenEJB -- service jboss.naming.context.java.ejb.OMGenEJB]
  
  00:36:25,671 ERROR [stderr] (default task-2)    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:157)
  
  ... ... 39 more
  
  00:36:25,672 ERROR [stderr] (default task-2) Caused by: java.lang.RuntimeException: javax.naming.NameNotFoundException: ejb/OMGenEJB -- service jboss.naming.context.java.ejb.OMGenEJB
  
  00:36:25,672 ERROR [stderr] (default task-2)    at org.jboss.as.ejb3.deployment.processors.EjbLookupInjectionSource$1.getReference(EjbLookupInjectionSource.java:99)
  
  00:36:25,672 ERROR [stderr] (default task-2)    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:143)
  
  00:36:25,676 ERROR [stderr] (default task-2)    ... 44 more
  
  00:36:25,676 ERROR [stderr] (default task-2) Caused by: javax.naming.NameNotFoundException: ejb/OMGenEJB -- service jboss.naming.context.java.ejb.OMGenEJB
  
  00:36:25,676 ERROR [stderr] (default task-2)    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
  
  00:36:25,677 ERROR [stderr] (default task-2)    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
  
  00:36:25,677 ERROR [stderr] (default task-2)    at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:235)
  
  00:36:25,677 ERROR [stderr] (default task-2)    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
  
  00:36:25,677 ERROR [stderr] (default task-2)    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
  
  00:36:25,677 ERROR [stderr] (default task-2)    at javax.naming.InitialContext.lookup(InitialContext.java:417)
  
  00:36:25,677 ERROR [stderr] (default task-2)    at javax.naming.InitialContext.lookup(InitialContext.java:417)
  
  00:36:25,677 ERROR [stderr] (default task-2)    at org.jboss.as.ejb3.deployment.processors.EjbLookupInjectionSource$1.getReference(EjbLookupInjectionSource.java:81)
  
  00:36:25,677 ERROR [stderr] (default task-2)    ... 45 more
  </code></pre>
  
  <p>The solution that <strong>works</strong> is using the global or app names, the problem is it contains the ear name and version...</p>
  
  <pre><code>"java:global/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome"
  "java:app/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome"
  </code></pre>
  
  <p>These <strong>don't work</strong></p>
  
  <pre><code>"java:module/OMGenEJB!com.irisel.oms.ejb.browser.OMGenBean"
  "java:module/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome"
  "java:comp/env/ejb/OMGenEJB"
  </code></pre>
  
  <p>I would like to check with you if you think this is compliant with the Java-7 specification.</p>
  
  <p>Thanks</p>