World Library  
Flag as Inappropriate
Email this Article

Java Naming and Directory Interface

Article Id: WHEBN0000042762
Reproduction Date:

Title: Java Naming and Directory Interface  
Author: World Heritage Encyclopedia
Language: English
Subject: Datasource, Service locator pattern, Apache Aries, Enterprise JavaBeans, Java version history
Collection: Application Layer Protocols, Java Apis, Java Enterprise Platform
Publisher: World Heritage Encyclopedia

Java Naming and Directory Interface

The Java Naming and Directory Interface (JNDI) is a Java API for a directory service that allows Java software clients to discover and look up data and objects via a name. Like all Java APIs that interface with host systems, JNDI is independent of the underlying implementation. Additionally, it specifies a service provider interface (SPI) that allows directory service implementations to be plugged into the framework. It may make use of a server, a flat file, or a database; the choice is up to the vendor.

Typical uses of JNDI include:

  • connecting a Java application to an external directory service (such as an address database or an LDAP server)
  • allowing a Java Servlet to look up configuration information provided by the hosting web container[1]


  • Background 1
  • Basic lookup 2
  • Searching 3
  • References 4
  • External links 5


The Java RMI and Java EE APIs use the JNDI API to look up objects in a network.

The API provides:

  • a mechanism to bind an object to a name
  • a directory-lookup interface that allows general queries
  • an event interface that allows clients to determine when directory entries have been modified
  • LDAP extensions to support the additional capabilities of an LDAP service

The SPI portion allows support for practically any kind of naming or directory service, including:

Sun Microsystems first released the JNDI specification on March 10, 1997.[2] As of 2006, the current version is JNDI 1.2.

Basic lookup

JNDI (Java Naming and Directory Interface) organizes its names into a hierarchy. A name can be any string such as "com.mydomain.ejb.MyBean". A name can also be an object that implements the Name interface, however a string is the most common way to name an object. A name is bound to an object in the directory by storing either the object or a reference to the object in the directory service identified by the name.

The JNDI API defines a context that specifies where to look for an object. The initial context is typically used as a starting point.

In the simplest case, an initial context must be created using the specific implementation and extra parameters required by the implementation. The initial context will be used to look up a name. The initial context is analogous to the root or top of a directory tree for a file system. Below is an example of creating an initial context:

Hashtable contextArgs = new Hashtable();

// First you must specify the context factory.
// This is how you choose between jboss implementation
// vs. an implementation from Sun or other vendors.
contextArgs.put( Context.INITIAL_CONTEXT_FACTORY, "com.jndiprovider.TheirContextFactory" );

// The next argument is the URL specifying where the data store is:
contextArgs.put( Context.PROVIDER_URL, "jndiprovider-database" );

// (You may also have to provide security credentials)

// Next you create the initial context
Context myCurrentContext = new InitialContext(contextArgs);

A context is then used to look up previously bound names in that context. For example:

MyBean myBean = (MyBean)  myCurrentContext.lookup("com.mydomain.MyBean");

Alternative to above code is as below:

The Context object can also be configured by adding file in classpath containing initial context factory class name and provider URL. The above code will be reduced as shown below:

//just need to create initial context object, it will try to read file from the classpath.
Context myCurrentContext = new InitialContext();

A context is then used to look up previously bound names in that context. For example:

MyBean myBean = (MyBean)  myCurrentContext.lookup("com.mydomain.MyBean");


Attributes may be attached to special entries called directories. Directories enable searching for objects by their associated attributes. Directories are a type of context; they restrict the name space much like a directory structure on a file system does.


  1. ^ "JNDI Resources HOW-TO". Apache Tomcat 7 User Guide. Apache Software Foundation. Retrieved 21 January 2014. 
  2. ^ [1]

External links

  • Java SE 7 JNDI page
  • The JNDI Tutorial
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.