Call the Context.lookup method to resolve a Bean’s home name to a proxy for the Bean’s home interface. If the server where the Bean is installed has a name context configured, pass the server’s name context as part of the Bean home name, in the format:
Server-name-context/Bean-home
Call javax.rmi.PortableRemoteObject.narrow to narrow the returned object to the Bean’s 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 home name cannot be resolved or the home interface proxy cannot be created. This can happen for any of the following reasons:
The server address specified with the Context.PROVIDER_URL property is incorrect or the server is not running.
Authentication with the specified credentials failed.
The Bean is incorrectly configured on the server. For example, a skeleton has not been generated, or the Bean’s properties specify the wrong implementation class.
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 home name of test/p1/Stateless1:
import test.p1.*; import javax.naming.*; 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()); }