The meaning of NoInitialContextException error The meaning of NoInitialContextException error java java

The meaning of NoInitialContextException error


The javax.naming package comprises the JNDI API. Since it's just an API, rather than an implementation, you need to tell it which implementation of JNDI to use. The implementations are typically specific to the server you're trying to talk to.

To specify an implementation, you pass in a Properties object when you construct the InitialContext. These properties specify the implementation to use, as well as the location of the server. The default InitialContext constructor is only useful when there are system properties present, but the properties are the same as if you passed them in manually.

As to which properties you need to set, that depends on your server. You need to hunt those settings down and plug them in.


You should set jndi.properties. I've given below some piece of code that explain how the properties are set for activemq. Like that you can set for your application. Inside a J2EE container like JBoss no need to set these properties.

Properties props = new Properties();props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");InitialContext ctx = new InitialContext(props);// get the initial context// InitialContext ctx = new InitialContext();QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");        // create a queue connectionQueueConnection queueConn = connFactory.createQueueConnection();   queueConn.start();// lookup the queue objectQueue queue = (Queue) ctx.lookup("dynamicQueues/Payment_Check");  

I know this is a late answer, but just giving for future reference.


you need to put the following name/value pairs into a hash table and call this constructor:

public InitialContext(Hashtable<?,?> environment)

the exact values depend on your application server, this example is for jboss

jndi.java.naming.provider.url=jnp://localhost:1099/jndi.java.naming.factory.url=org.jboss.naming:org.jnp.interfacesjndi.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory