![]() ![]() ![]() ![]() |
Examples |
This example shows you how to write a program that looks up an object whose name is passed in as a command-line argument. This example uses a service provider for the file system. Therefore, the name that you supply to the program must be a file name. It is not necessary to understand details about the service provider at this point.Import the JNDI Packages
Using your favorite text editor, create a file named Lookup.java. You can either import the entire package or individual classes and interfaces. The following code imports each class that is used from the javax.namingpackage:
import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException;Create an Initial Context
In the main() method of the program, first create an initial context. You indicate that you're using the file system service provider by setting the environment properties parameter (represented by a Hashtable class) to the InitialContextconstructor appropriately:
Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); Context ctx = new InitialContext(env);Setting up the parameters for this constructor is explained in more detail in The Basics
trail.
Look Up an Object
Then, use Context.lookup()to look up an object. This code looks up the object bound to the name supplied in the command line.
Object obj = ctx.lookup(name);Catch NamingException
The creation of the initial context and the lookup() method can throw a NamingException. For this reason, you need to enclose these calls inside a try block. Here's the code fragment repeated with the try block:
try { // Create the initial context Context ctx = new InitialContext(env); // Look up an object Object obj = ctx.lookup(name); // Print it out System.out.println(name + " is bound to: " + obj); } catch (NamingException e) { System.err.println("Problem looking up " + name + ": " + e); }Compile the Program
Next, you need to compile the source file using the Java compiler. In order to compile to program, you need to have access to the JNDI classes. If you are using the Java 2 SDK, v 1.3, the JNDI classes are already included. Otherwise, you can include the classes by setting the CLASSPATH variable to include the jndi.jar that you've downloaded from the JNDI Web site, or install jndi.jar as an installed extension. See the Preparationslesson for details on how to install the JNDI classes and service providers.
If the compilation succeeds, the compiler creates a file named
Lookup.class
in the same directory (folder) as the Java source file (Lookup.java
).If the compilation fails, make sure you typed in and named the program exactly as shown above, using the capitalization shown. If you are still having problems, see Common Problems
for help.
Run the Program
You need to have access to the JNDI classes and your example classes. If you are using the Java 2 SDK, v 1.3, the JNDI classes are already included. Otherwise, include the JNDI classes by setting the CLASSPATH variable to include both jndi.jar and the directory in which you have your Lookup.class file. You also need to place the file system service provider classes (fscontext.jar) in your CLASSPATH. You only need to have jndi.jar and fscontext.jar in your CLASSPATH if you have not installed the JNDI as an installed extension. See the Preparationslesson for details on how to install the JNDI classes and service providers. Note that the archive fscontext.jar is not included with the Java 2 SDK, v 1.3.
To run the program, supply the name of a file in your file system:
Or# java Lookup /tmpIf you supply a file directory, you will see something like the following:# java Lookup c:\autoexec.batIf the name that you supplied is a file, you will see something like this:# java Lookup /tmp /tmp is bound to: com.sun.jndi.fscontext.RefFSContext@1dae083fIf you have any trouble running this example, see Common Problems/tmp/f is bound to: //tmp/f.
![]() ![]() ![]() ![]() |
Examples |