Error NoInitialContextException

I am writing a client for my EJB, and trying to execute it, I get the following exception:

javax.naming.NoInitialContextException: You must specify the class name in the environment or system, either as an applet parameter or in the application resource file.

I just don’t understand what the problem is.

+41
java java-ee jndi
Oct 06 '09 at 12:50
source share
12 answers

The javax.naming package contains the JNDI API. Since this is just an API, not an implementation, you need to tell which JNDI implementation to use. Implementations are usually specific to the server you are trying to talk to.

To specify an implementation, you pass a Properties object when you create an InitialContext . These properties determine the implementation used, as well as the location of the server. By default, the InitialContext constructor is only useful if you have system properties, but the properties are the same as if you were passing them manually.

As for the properties that need to be set, it depends on your server. You need to track these settings and connect them.

+26
Jan 01 '09 at 17:33
source share

Is a JNDI issue. You will see this exception if the InitialContext class does not have default properties for the JNDI service provider or explicitly configured server properties.

Set the environment property Context.INITIAL_CONTEXT_FACTORY for the name of the initial context implementation class that you are using. This class should be available to your program in the classpath.

Check:

http://docs.oracle.com/javase/7/docs/api/javax/naming/InitialContext.html

http://java.sun.com/products/jndi/tutorial/getStarted/TOC.html (runtime problems)

+9
06 Oct '09 at 13:06
source share

you need to put the following name / value pairs in a Hashtable 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.interfaces jndi.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory 
+9
Dec 31 '09 at 16:29
source share

You must install jndi.properties. I have provided some code snippets below that explain how the properties for activemq are set. How can you install for your application. Inside a J2EE container, such as JBoss, you do not 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 connection QueueConnection queueConn = connFactory.createQueueConnection(); queueConn.start(); // lookup the queue object Queue queue = (Queue) ctx.lookup("dynamicQueues/Payment_Check"); 

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

+7
Nov 18 '13 at 7:38
source share

In particular, I got this problem when trying to get the default (no-args) InitialContext in an embedded Tomcat7 instance in SpringBoot.

The solution for me was to say Tomcat enableNaming .

i.e.

 @Bean public TomcatEmbeddedServletContainerFactory tomcatFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer( Tomcat tomcat) { tomcat.enableNaming(); return super.getTomcatEmbeddedServletContainer(tomcat); } }; } 
+3
May 05 '16 at 2:04
source share

A simple and custom solution creates a single jndi.properties file and puts this file in the classpath. jndi.properties can be created as

 java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory # use the following property to configure the default connector java.naming.provider.url = vm://localhost # use the following property to specify the JNDI name the connection factory # should appear as. #connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry # register some queues in JNDI using the form # queue.[jndiName] = [physicalName] queue.MyQueue = example.MyQueue # register some topics in JNDI using the form # topic.[jndiName] = [physicalName] topic.MyTopic = example.MyTopic 

Just provide your factory name and url and put this file in your classpath. JMS will receive the necessary information on its own and is easy to configure in the future.

+2
Aug 11 '15 at 10:06
source share

In most cases, these parameters are also defined in the jndi.properties file. Do you have someone who is lying somewhere?

+1
Aug 11 2018-10-10T00:
source share

My problem with this was that I was creating a hibernation session, but the JNDI settings for my database instance were incorrectly configured due to a classpath issue. Just FYI ...

+1
Oct 27 '11 at 17:47
source share

I solved the same problem by adding the following Jar libraries to the project:

  • Appserv-rt.jar
  • javaee.jar

from folder: C:\Program Files\glassfish-4.0\glassfish\lib

Links to these libraries were broken, and Netbeans did not find suitable classes.

+1
Apr 30 '14 at 13:36 on
source share

make sure that dependencies are included for naming berths and a berth plus (not only the scope provided). This fixed it for me.

0
Aug 01 '13 at 18:01
source share

Do it:

 Properties props = new Properties(); props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory"); Context initialContext = new InitialContext(props); 

Also add this to the project libraries:

C:\installs\glassfish\glassfish-4.1\glassfish\lib\gf-client.jar adjust the path accordingly

0
Sep 14 '15 at 15:01
source share

you need to use jboss-client.jar in your client project and you need to use jnp-client jar in ejb project

0
07 Sep '16 at 11:00
source share



All Articles