Previous | Next | Trail Map | Getting Started | Examples

Naming Example

This example shows you how to write a program that looks up an object whose name is passed in as a command-line argument. It uses a service provider for the file system. Therefore the name that you supply to the program must be a filename. You do not need to understand details about the service provider at this point.

Importing the JNDI Classes

Using your favorite text editor, create a file named Lookup.java. You can import either the entire package or only individual classes and interfaces. The following code imports each class that is used from the javax.naming(in the API reference documentation) package.
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

Creating an Initial Context

In the main() method of the program, create an initial context. Indicate that you're using the file system service provider by setting the environment properties parameter (represented by a Hashtable class) to the InitialContext(in the API reference documentation) constructor, as follows.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, 
    "com.sun.jndi.fscontext.RefFSContextFactory");

Context ctx = new InitialContext(env);

How to set up the parameters for this constructor is explained in more detail in The Basics (in the Basics trail) trail.

Looking Up an Object

Next, use Context.lookup()(in the API reference documentation) to look up an object. The following code looks up the object bound to the name supplied in the command line.
Object obj = ctx.lookup(name);

Catching NamingException

The creation of the initial context and the lookup() method can throw a NamingException(in the API reference documentation). For this reason, you need to enclose these calls inside a try/catch clause. Here's the code fragment repeated with the try/catch clause.
try {
    // Create the initial context
    Context ctx = new InitialContext(env);

    // Look up an object
    Object obj = ctx.lookup(name);

    // Print it
    System.out.println(name + " is bound to: " + obj);
	    
} catch (NamingException e) {
    System.err.println("Problem looking up " + name + ": " + e);
}

Compiling the Program

Next, you compile the source file using the Java compiler. To compile to program, you must have access to the JNDI classes. If you are using the Java 2 SDK, v1.3, then the JNDI classes are already included. Otherwise, you can include the classes either by setting the CLASSPATH variable to include the jndi.jar that you downloaded from the JNDI Web site or by installing jndi.jar as an installed extension. See the Preparations (in the Basics trail) lesson for details on how to install the JNDI classes and service providers.

If the compilation succeeds, then the compiler will create a file named Lookup.class in the same directory (folder) as the Java source file (Lookup.java). If the compilation fails, then make sure that you typed in and named the program exactly as shown here, using the capitalization shown. If you are still having problems, then see the Common Problems (in the Getting Started trail) lesson for help.

Running the Program

To run the program, you need access to the JNDI classes, the file system service provider, and your example class (Lookup.class). See the compilation step for instructions on including access to the JNDI classes. To include the file system service provider classes (fscontext.jar and providerutil.jar), either include them in your CLASSPATH variable or install them as extensions. Note that these archive files are not included with the Java 2 SDK, v1.3. See the Preparations (in the Basics trail) lesson for details on how to install the JNDI classes and service providers. Finally, include the directory that contains your Lookup.class file in your the CLASSPATH variable.

To run the program, supply the name of a file in your file system, as follows:

# java Lookup /tmp
Or as follows:
# java Lookup \autoexec.bat
If you supply a file directory, then you will see something like the following.
# java Lookup /tmp
/tmp is bound to: com.sun.jndi.fscontext.RefFSContext@1dae083f
If the name that you supplied is a file, then you will see something like this:
/tmp/f is bound to: //tmp/f
If you have any trouble running this example, then see Common Problems (in the Getting Started trail).


Previous | Next | Trail Map | Getting Started | Examples