Troubleshooting
Problem
When writing a J2SE program that performs java:comp lookups using the embeddable EJB container, you must remember that java:comp is only accessible within an EJB method. If you attempt to lookup a java:comp name directly from the main method, you will get an NMSV0307E error.
Symptom
NMSV0307E: A java: URL name was used, but Naming was not configured to handle java: URL names. The likely cause is a user in error attempting to specify a java: URL name in a non-J2EE client or server environment. Throwing ConfigurationException.
javax.naming.ConfigurationException: Name space accessor for the java: name space has not been set. Possible cause is that the user is specifying a java: URL name in a JNDI Context method call but is not running in a J2EE client or server environment.
at com.ibm.ws.naming.java.javaURLContextFactory.isNameSpaceAccessable
at com.ibm.ws.naming.urlbase.UrlContextFactory.getObjectInstance
at javax.naming.spi.NamingManager.getURLContext
at javax.naming.InitialContext.getURLOrDefaultInitCtx
at javax.naming.InitialContext.lookup
at com.ibm.test.EmbeddableEJBTest.main
Cause
Performing a java:comp lookup outside the context of an EJB.
Environment
WebSphere Application Server version 8 is the first version of the product to include an embeddable EJB container for use in a J2SE program.
Diagnosing The Problem
The following code sample does not work because a lookup of a java:comp name is performed outside the context of an EJB.
public static void main(String[] args) throws Exception {
EJBContainer ec = EJBContainer.createEJBContainer(properties);
Context c = ec.getContext();
DataSource ds = (DataSource)c.lookup("java:comp/env/jdbc/myDS");
}
Resolving The Problem
java:comp is only active while an EJB method is active. One pattern is to use Runnable to propagate your logic into the context of an EJB method:
@Stateless
public class TestBean {
public void invoke(Runnable r) {
r.run();
}
}
public static void main(String[] args) {
...
Context c = ec.getContext();
TestBean bean = c.lookup(".../TestBean");
bean.invoke(new Runnable() {
public void run() {
DataSource ds = (DataSource)new InitialContext().lookup(
"java:comp/env/jdbc/myDS");
...
}
});
}
Was this topic helpful?
Document Information
Modified date:
15 June 2018
UID
swg21580598