![]() ![]() ![]() ![]() |
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 the JNDI classes in your classpath. You can do that by setting the CLASSPATH variable to include the jndi.jar that you've downloaded from the JNDI Web site.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 the JNDI classes and your example classes in your classpath. You can do that 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.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 |