views:

391

answers:

4

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. I've been at this for a long time and still can't figure it out.

+1  A: 

Is a JNDI problem. You will see that exception if the InitialContext class has neither default properties for the JNDI service provider nor explicitly configured server properties.

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

Check:

http://java.sun.com/j2se/1.3/docs/api/javax/naming/InitialContext.html

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

JuanZe
thx for the pointer, how can I solve it ? I'm new to this J2EE stuff.
Attilah
Is not a question for a quick answer. Depends on your application server stuff. I suggest you check the documentation for the app server you are using and try to run a Hello World EJB app on your plataform to get deeper understanding of the topic.
JuanZe
+2  A: 

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
stacker
+1  A: 

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.

skaffman
A: 

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

Maarten van Leunen