NoInitialContextException error

2019-01-03 03:03发布

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

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file.

I just can't understand what the problem is.

12条回答
再贱就再见
2楼-- · 2019-01-03 03:14

Do this:

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 libraries of the project:

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

查看更多
虎瘦雄心在
3楼-- · 2019-01-03 03:20

you need to put the following name/value pairs into a Hashtable and call this contructor:

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
查看更多
疯言疯语
4楼-- · 2019-01-03 03:24

Easy & configurable solution is create one jndi.properties file and put this file in 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 specify your naming factory & url and put this file in your classpath. JMS will fetch required info by itself and it's easily configurable in future also.

查看更多
小情绪 Triste *
5楼-- · 2019-01-03 03:25

Most of the time these settings are also defined in a jndi.properties file. Do you have that one lying around somewhere?

查看更多
做个烂人
6楼-- · 2019-01-03 03:25

make sure dependencies for jetty naming and jetty plus are included (not just provided scope). This fixed it for me.

查看更多
一纸荒年 Trace。
7楼-- · 2019-01-03 03:29

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

  • appserv-rt.jar
  • javaee.jar

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

The links to these libraries were broken and Netbeans didn't found the right classes to use.

查看更多
登录 后发表回答