title: Consuming local EJB, in the same Container but different ears
body: <p>I'm triying to consume a Local EJB in the same Glassfish, but different ears. But Glassfish can't found the local EJB or can't consume</p>
  
  <p>I read this:
  <strong>According to the JavaEE tutorial, the client of a @Local bean "must run in the same JVM as the enterprise bean it accesses."</strong></p>
  
  <p><strong>In the first ear</strong>, 
      I have the local Interface inside a jar</p>
  
  <pre><code>@Local
  public interface MyLocalBean {
    int getNumber(int num3);
  } 
  </code></pre>
  
  <p>In another jar, I have  the implementation</p>
  
  <pre><code>@Stateless
  @LocalBean
  public class MyLocalBeanImpl implements MyLocalBean,Serializable{
       public MyLocalBeanImpl() {}
       public int getNumber(int num3){......
  </code></pre>
  
  <p><strong>In the second ear</strong>, 
  in the same Glassfish</p>
  
  <p>I have the local Interface inside a jar</p>
  
  <pre><code>@Local
  public interface MyLocalBean {
  int getNumber(int num3);
   } 
  </code></pre>
  
  <p>In another jar, I have the consumer</p>
  
  <pre><code>@Stateless
  @LocalBean
  public class BeanConsumer{
  @EJB(name="MyLocalBeanImpl")
  private MyLocalBean beanlocal;
  </code></pre>
  
  <p>with @EJB and  (name="MyLocalBeanImpl") parameter the error is:</p>
  
  <pre><code>Cannot resolve reference Local ejb-ref name=MyLocalBeanImpl,Local 3.x 
  interface =my.package.MyLocalBean,ejb-link=null,lookup=,
  mappedName=,jndi-name=,refType=Session|#]
  </code></pre>
  
  <p>with @EJB and  (name="my.package.MyLocalBeanImpl") parameter the error is:</p>
  
  <pre><code>Cannot resolve reference Local ejb-ref name=my.package.MyLocalBeanImpl,
  Local 3.x interface =my.package.MyLocalBean,ejb-link=null,
  lookup=,mappedName=,jndi-name=,refType=Session|#]***
  </code></pre>
  
  <p>I tried too with mappedName</p>
  
  <pre><code>    @Stateless(name="MyLocalBeanImpl",mappedName ="MyLocalBeanImpl")
      public class MyLocalBeanImpl implements MyLocalBean{
  
  @EJB(name="MyLocalBeanImpl",mappedName ="MyLocalBeanImpl")
  private MyLocalBean beanlocal;
  </code></pre>
  
  <p>but the error persist</p>
  
  <pre><code> Cannot resolve reference Local ejb-ref name=MyLocalBeanImpl,
   Local 3.x interface =my.package.MyLocalBean,ejb-link=null,
   lookup=,mappedName=MyLocalBeanImpl,jndi-name=,refType=Session
  </code></pre>
  
  <p>GlassFish works with a JNDI like</p>
  
  <pre><code> java:global[/&lt;app-name&gt;]/&lt;module-name&gt;/&lt;bean-name&gt;
  </code></pre>
  
  <p>I added the annotation @LocalBean to the bean MyLocalBeanImpl </p>
  
  <pre><code>@Stateless
  @LocalBean
  public class MyLocalBeanImpl implements MyLocalBean{
       public MyLocalBeanImpl() {}
       public int getNumber(int num3){......
  </code></pre>
  
  <p>and when I deployed Glassfish say the jndi are two</p>
  
  <pre><code>  Portable JNDI names for EJB OtroBean
    java:global/myfirstear/myejbs/MyLocalBeanImpl!my.package.MyLocalBeanImpl  
    and
    java:global/myfirstear/myejbs/MyLocalBeanImpl!my.package.MyLocalBean
  </code></pre>
  
  <p>i'm trying with</p>
  
  <pre><code> @EJB(lookup="java:global/myfirstear/myejbs/MyLocalBeanImpl!my.package.MyLocalBeanImpl")
  </code></pre>
  
  <p>It seems that Glassfish found the Local Bean, but... Glassfish have problems casting o something like this</p>
  
  <pre><code> javax.naming.NamingException
   javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
   at     
   com.sun.ejb.containers.StatelessSessionContainer
   ._getContext(StatelessSessionContainer.java:454)
   .....
   Caused by: javax.ejb.EJBException: 
   javax.ejb.CreateException: Could not create stateless EJB
   ....
   Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: 
   Exception attempting to inject Local ejb-ref name=my.package.BeanConsumer/beanlocal,
   Local 3.x interface =my.package.MyLocalBean,ejb-link=null,
   lookup=java:global/myfirstear/myejbs/MyLocalBeanImpl!my.package.MyLocalBeanImpl,
   mappedName=,jndi-name=,refType=Session into class my.package.BeanConsumer: 
   Can not set my.package.MyLocalBean field my.package.BeanConsumer.beanlocal 
   to   my.package.beans.__EJB31_Generated__MyLocalBeanImpl__Intf____Bean__
  </code></pre>
  
  <p>And trying with</p>
  
  <pre><code> @EJB(lookup="java:global/myfirstear/myejbs/MyLocalBeanImpl")
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>throws javax.naming.NamingException
   javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
  
   Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: 
   Exception  attempting to inject Local ejb-ref   
   name=my.package.BeanConsumer/beanlocal,
   Local 3.x  interface =my.package.MyLocalBean,
   ejb-link=null,
   lookup=java:global/myfirstear/myejbs/MyLocalBeanImpl,
   mappedName=,jndi-name=,refType=Session 
   into class  my.package.BeanConsumer: 
   Lookup failed for   'java:comp/env/my.package.BeanConsumer/beanlocal' 
   in SerialContext
   [myEnv={java.naming.factory.initial=
   com.sun.enterprise.naming.impl.SerialInitContextFactory, 
   java.naming.factory.state=
   com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,
   java.naming.factory.url.pkgs=
   com.sun.enterprise.naming}
  </code></pre>