Search code examples
javajakarta-eejava-ee-6glassfish-3ejb-3.1

Looking up an EJB dynamically


I'm developing an application on Glassfish 3. I have an EJB that looks like this:

@LocalBean
@Stateless
public class MyBean {
  public void doSomething() {}
}

My client code (running inside the same application) looks like this:

MyBean mb = (MyBean) InitialContext.doLookup(MyBean.class.getName());

According to a few sources, this should be a valid lookup method, but it throws a NameNotFoundException. What am I doing wrong?


Solution

  • According to what sources? I would personally use portable JNDI names specified in EJB 3.1. See:

    Depending on the packaging, I would use a module-scoped or an application-scoped JNDI name.

    BTW, GlassFish logs the portable JNDI names at deployment time.