This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes InitialContext and JNDI confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "InitialContext and JNDI confusion" Watch "InitialContext and JNDI confusion" New topic

InitialContext and JNDI confusion

Mike Scherr

Joined: Sep 18, 2003
Posts: 3
I'm a little new to the whole EJB thing and I'm just trying to get a helloworld session bean to work and have hit a wall.
I'm using the 1.4 Beta J2EE SDK from Sun. I ran the asadmin server and the server starts up ok. I point my browser to localhost:4848 and I get the display page. Check. I also deployed their sample jsp page and that worked too so I'm assuming the server is running and working the way it should.
Now for the part that doesn't work. I made the helloworld session bean and then made my TestClient program. Now, in the documentation it says that when you use the deploytool on your .ear file (which I made with the delpoytool) you need to get it to return a client jar. There is a little check on the deploytool for this operation and I check the box when its deploying my .ear file. Now, shouldn't I get a .jar file in return? NO matter what I type in that stupid field I can't get it to produce a jar file. From what I read, I need to include this .jar file in the classpath when I run the client or it won't be able to connect to the sessionbean.
Now, after about 9 hours of playing with the stupid thing, I finally get it to produce a .jar file for me. But, I called it helloClient.jar and it gives me helloworldClient.jar? I'm totally confused. But anyway, I include the stupid .jar file in my classpath for my TestClient program anyway, just to see what happens.
I run my TestClient and it throws an InitialContext error. Says I need to pass it an initialcontxt. Alright. I create a hashtable and pass it 3 different values in 3 different tests to see what happens.
I pass the following,
they all throw a different exception but what its saying is it can't connect on port 389 I believe it is. Firewall is disabled so this shouldn't be a problem.
Now, reading through some books and things, they tell me you have to supply your own InitialContext which is dependent on what server you're running.
Well, I can't find this anywhere in any documentation for Sun's J2EE asadmin server. When you run that asadmin server tool, what is the default InitialConext? Is it running an LDAP by default? I can't find it.
Or is there no naming service included at all and I have to download something else yet on top of it? You'd think they'd have a naming service included.

Here's the TestHello Program. NOthing earth shattering
import java.util.*;
import javax.naming.*;
import javax.rmi.*;
public class TestHello
public static void main(String[] args)
/** Creates a JNDI naming context for location objects */

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");


Context context = new InitialContext(env);

/** Asks the context to locate an object named "HelloWorld" and expects the
object to implement the HelloWorldSessionHome interface */

HelloWorldSessionHome home = (HelloWorldSessionHome)

/** Asks the Home interface to create a new session bean */
HelloWorldSession session = (HelloWorldSession) home.create();
System.out.println("The default greeting is: "+
System.out.println("The greeting is now: "+session.getGreeting());
/** Destroy this session */
/** Now create a session with a different greeting */
session = (HelloWorldSession) home.create("Guten Tag!");
System.out.println("Created a new session with a greeting of: "+
/** Destroy this session */
catch (Exception exc)
Here is the stacktrace once the client is provided with an InitialContext.
I'm running this from Eclipse 2.0 and 1.4.1 JDK.

javax.naming.CommunicationException: localhost:389. Root exception is Connection refused: connect
at Method)
at com.sun.jndi.ldap.Connection.createSocket(
at com.sun.jndi.ldap.Connection.<init>(
at com.sun.jndi.ldap.LdapClient.<init>(
at com.sun.jndi.ldap.LdapClient.getInstance(
at com.sun.jndi.ldap.LdapCtx.connect(
at com.sun.jndi.ldap.LdapCtx.<init>(
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(
at javax.naming.spi.NamingManager.getInitialContext(
at javax.naming.InitialContext.getDefaultInitCtx(
at javax.naming.InitialContext.init(
at javax.naming.InitialContext.<init>(
at TestHello.main(

Help a noob please.
Mike Scherr

Joined: Sep 18, 2003
Posts: 3
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
I would not use "com.sun.jndi.ldap.LdapCtxFactory" for this. Usually you have to specify a LDAP server address with that (unless it's local). I think you need to look into the server and see how it binds the interface, and what ctxfatcory to use... Doesn't sound like it uses LDAP. Sorry - I have no details. Please post a solution when found!!!
Maybe you could use JBoss instead. It's pretty straight forward. There is lots of support and documentation.
Mike Scherr

Joined: Sep 18, 2003
Posts: 3
I downloaded Jboss but using a new server seems like a roundabout way of solving the problem. In fact, when I've solved it, I'll probably just install and use Jboss anyway but I'd still like to know. I'm sure someone somewhere must know what the default InitialContext of Sun's J2EE server is. Yes I'm running this on localhost.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Mike, the J2EE SDK discussion forum at might have what you're looking for.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Harjot Narula

Joined: Sep 22, 2003
Posts: 11
Try using this instead
or try this
I think the latter should work for you.
wish u luck
Bill Dudney
Ranch Hand

Joined: Sep 05, 2003
Posts: 234
Hi Mike,
Yes this junk can be very frustrating
I think the to keep in mind here is that the InitialContext is expecting for you to tell it which class to instanciate as well as any other connection parameters that are needed to open an connection and work with the JNDI provider. Since it can be just about anything it is often very hard to get the client side config correct.
Something I often do when running a new sever is grab the file to discover what real properties are required to be set for the initial context to run. JBoss 3.2 has them in its sever conf directories (i.e. $JBOSS/server/default/conf/ Other app servers do other thing.
This document JNDI Tutorial describes the various ways you can get the JNDI env set up properly.
The J2EE RI (1.3.1 & 1.4) from sun has a file in the $RI_HOME/lib/j2ee.jar file.
The values are
If you put this j2ee.jar file in your classpath you should be able to use simply

on the client side. If you can't or don't want j2ee.jar on your client classpath then you can specify the two properties like this

Hope this helps.
[ September 23, 2003: Message edited by: Bill Dudney ]

TTFN,<br /> <br />-bd-<br /> <br /><a href="" target="_blank" rel="nofollow">Jakarta Pitfalls</a> | <a href="" target="_blank" rel="nofollow">J2EE AntiPatterns</a> | <a href="" target="_blank" rel="nofollow">Mastering JavaServer Faces</a> | <a href="" target="_blank" rel="nofollow">Blog</a> | <a href="" target="_blank" rel="nofollow">Eclipse Blog</a> | <a href="" target="_blank" rel="nofollow">Eclipse 3 Live</a>
Consider Paul's rocket mass heater.
subject: InitialContext and JNDI confusion
Similar Threads
jndi hell !!
Exception at client side EJB program
LDAP server Access Error in java
Attemplting to connect to a public LDAP directory
Unable to create a JNDI InitialContext problem