View RSS Feed


A JNDI Example

Rate this Entry
by , 07-07-2012 at 05:06 PM (801 Views)
In naming systems, an object could be accessed by creating initial context for sake of a naming system so that an entry point could be gained into a naming system. Once initial context is present, an object could be looked up by name.

Java Code:
import java.util.Properties;
import javax.naming.*;

public class Lookup {        
  public static void main(String[] args) {        
    String name = "";
    if (args.length > 0) 
      name = args[0];
    try {
      // Create a Properties object and set properties appropriately
      Properties props = new Properties();
      props.put(Context.PROVIDER_URL, "file:///");
      // Create the initial context from the properties we just created
      Context initialContext = new InitialContext(props);
      // Look up the object
      Object obj = initialContext.lookup(name);
      if (name.equals(""))
        System.out.println("Looked up the initial context");
        System.out.println(name + " is bound to: " + obj);
    catch (NamingException nnfe) {
      System.out.println("Encountered a naming exception");
Lookup application creates the java.util.Properties and stores String values by using it. Keys that would be used for such values would be constant that are defined in class javax.naming.Context. Every constant will be corresponding with JNDI property name which will communication information regarding JNDI service in use. Factory class is specified by INITIAL_CONTEXT_FACTORY which will ultimately be creating initial context for desired service.

Submit "A JNDI Example" to Facebook Submit "A JNDI Example" to Digg Submit "A JNDI Example" to Submit "A JNDI Example" to StumbleUpon Submit "A JNDI Example" to Google

Tags: jndi example Add / Edit Tags