title: Correct way to lookup local EJB in websphere - Getting ClassCastException
body: <p>I have an EJB which is exposed by both local and remote interfaces</p>
  
  <pre><code>package com.sam.enqueue;
  
  import javax.ejb.Local;
  import javax.ejb.Remote;
  import javax.ejb.Singleton;
  
  @Singleton
  @Local(SamEnqueueLocal.class)
  @Remote(SamEnqueueRemote.class)
  public class SamEnqueue implements SamEnqueueRemote, SamEnqueueLocal {
  
  
  }
  
  
  // remote interface
  package com.sam.enqueue;
  
  import javax.ejb.Remote;
  
  @Remote
  public interface SamEnqueueRemote {
  
  
  }
  
  // local interface
  package com.sam.enqueue;
  
  @Local
  public interface SamEnqueueLocal {
  
  
  
  }
  </code></pre>
  
  <p>My app container is websphere 8.0 and I am not overriding the default JNDI names which the server assigns.
  During server startup I get the following default bindings in logs:</p>
  
  <pre><code>CNTR0167I: The server is binding the com.sam.enqueue.SamEnqueueRemote interface of the SamEnqueue enterprise bean in the SAM_ENQUEUE.jar module of the SAM_ENQUEUE application.  The binding location is: ejb/SAM_ENQUEUE/SAM_ENQUEUE.jar/SamEnqueue#com.sam.enqueue.SamEnqueueRemote
  CNTR0167I: The server is binding the com.sam.enqueue.SamEnqueueRemote interface of the SamEnqueue enterprise bean in the SAM_ENQUEUE.jar module of the SAM_ENQUEUE application.  The binding location is: com.sam.enqueue.SamEnqueueRemote
  CNTR0167I: The server is binding the com.sam.enqueue.SamEnqueueRemote interface of the SamEnqueue enterprise bean in the SAM_ENQUEUE.jar module of the SAM_ENQUEUE application.  The binding location is: java:global/SAM_ENQUEUE/SamEnqueue!com.sam.enqueue.SamEnqueueRemote
  CNTR0167I: The server is binding the com.sam.enqueue.SamEnqueueLocal interface of the SamEnqueue enterprise bean in the SAM_ENQUEUE.jar module of the SAM_ENQUEUE application.  The binding location is: ejblocal:SAM_ENQUEUE/SAM_ENQUEUE.jar/SamEnqueue#com.sam.enqueue.SamEnqueueLocal
  CNTR0167I: The server is binding the com.sam.enqueue.SamEnqueueLocal interface of the SamEnqueue enterprise bean in the SAM_ENQUEUE.jar module of the SAM_ENQUEUE application.  The binding location is: ejblocal:com.sam.enqueue.SamEnqueueLocal
  CNTR0167I: The server is binding the com.sam.enqueue.SamEnqueueLocal interface of the SamEnqueue enterprise bean in the SAM_ENQUEUE.jar module of the SAM_ENQUEUE application.  The binding location is: java:global/SAM_ENQUEUE/SamEnqueue!com.sam.enqueue.SamEnqueueLocal
  </code></pre>
  
  <p>The lookup class is just a simple java class in a different EAR in the same server with the following code:</p>
  
  <pre><code>Context ctx = new InitialContext();
  Object local = ctx.lookup("java:global/SAM_ENQUEUE/SamEnqueue!com.sam.enqueue.SamEnqueueLocal");
  
  SamEnqueueLocal samEnqueue = (SamEnqueueLocal) local;
  </code></pre>
  
  <p>The lookup is working with any of the three JNDI names for the local but it's not getting cast to <code>SamEnqueueLocal</code>. The exception trace is:</p>
  
  <pre><code>SystemErr     R java.lang.ClassCastException: com.sam.enqueue.EJSLocal0SGSamEnqueue_cf56ba6f incompatible with com.sam.enqueue.SamEnqueueLocal
  
  ... rest ommited
  </code></pre>
  
  <p>I've made a shared library and put the stub of destination EAR in it. The library is the classpath of the source lookup EAR with the <code>Classes loaded with local class loader first (parent last)</code> policy. The library is not isolated. If I remove the stub, I get a <code>java.lang.ClassNotFoundException: com.sam.enqueue.SamEnqueueLocal</code> as expected.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>While using Dependency Injection:</p>
  
  <pre><code>@EJB(lookup="ejblocal:com.sam.enqueue.SamEnqueueLocal")
  private SamEnqueueLocal samEnqueueLocal;
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>javax.ejb.EJBException: Injection failure; nested exception is: java.lang.IllegalArgumentException: Can not set com.sam.enqueue.SamEnqueueLocal field com.some.SomeBean.samEnqueueLocal to com.sam.enqueue.EJSLocal0SGSamEnqueue_cf56ba6f
  Caused by: java.lang.IllegalArgumentException: Can not set com.sam.enqueue.SamEnqueueLocal field com.some.SomeBean.samEnqueueLocal to com.sam.enqueue.EJSLocal0SGSamEnqueue_cf56ba6f
  </code></pre>
  
  <p>So it's basically the same.</p>