Resolving JNDI names

Call the Context.lookup method to resolve a bean’s JNDI name to a proxy for the bean’s home interface. If the server or cluster where the bean is installed has a name context configured, pass the server’s name context as part of the bean JNDI name, in the format:

Server-name-context/Bean-home

Where Server-name-context is the server’s initial naming context, and Bean-home is the component’s JNDI name, or, for server-side code executing in EJB or Web components, the aliased JNDI name in the calling component’s EJB reference properties.

Call javax.rmi.PortableRemoteObject.narrow to narrow the returned object to the bean’s home (or local home) interface class. narrow requires as parameters the object to be narrowed and a java.lang.Class reference that specifies the interface type to returned. To obtain the java.lang.Class reference, use Home.class, where Home is the bean’s home interface type. Cast the object returned by the narrow method to the bean’s Java home interface.

The lookup method throws javax.naming.NamingException if the bean JNDI name cannot be resolved or the home interface proxy cannot be created. This can happen for any of the following reasons:

Check the server’s log file if the cause of the error is not clear from the exception’s detail message.

The call below instantiates a proxy for a bean with Java home interface test.p1.Stateless1Home and bean JNDI name of test/p1/Stateless1:

import test.p1.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;

try {
  Object o = ctx.lookup("test/p1/Stateless1");
  Stateless1Home home = (Stateless1Home) 
    PortableRemoteObject.narrow(o, Stateless1Home.class);
} catch (NamingException ne) {
  System.out.println("Error: Naming exception: "
    + ne.getExplanation());
}