Using the lookup Method to Locate JNDI ResourcesNow that you've learned how to get the InitialContext , it's time to do something with it. That something is to look up objects and references to objects. Because we are using the file system service provider, the object that we will be using to look up will be a text file. Create a text file called foobar.txt and save the file into your jndi_root directory. You can add some text to the file if you want; it doesn't matter for our example. Now, let's use JNDI to look up this file and print out some information about it. Listing 4.6 shows a program that creates an InitialContext on our JNDI root directory and then performs a lookup on a name. The name is the name of the file foobar.txt . Listing 4.6 A Sample Program Using the lookup Methodimport java.io.File; import javax.naming.*; import java.sql.Timestamp; import java.util.Hashtable; import java.util.Properties; public class JNDILookupExample { // Default Constructor public JNDILookupExample(){ super(); } public void runExample( String fileName ){ Context initCtx = null; try { // Create an InitialContext using properties from resource files initCtx = new InitialContext(); // Perform the lookup File file = (File)initCtx.lookup( fileName ); // Print out something about the file long fileSize = file.length(); Timestamp ts = new Timestamp( file.lastModified() ); System.out.println( "File name: " + fileName ); System.out.println( "File size: " + fileSize + " bytes" ); System.out.println( "Last Modified: " + ts ); }catch (Exception ex ) { ex.printStackTrace(); }finally{ try{ System.out.println( "Closing the InitialContext" ); if ( initCtx != null ) initCtx.close(); }catch( Exception ex ){ System.out.println( "Could not close the InitialContext" ); } } } // Main used to get things going public static void main( String[] args ){ // Make sure the user passes in the filename if ( args.length != 1 ){ System.out.println( "Usage: JNDILookupExample <filename>" ); System.exit( 0 ); } String fileName = args[0]; JNDILookupExample example = new JNDILookupExample(); // Run the example example.runExample( fileName ); } } The example in Listing 4.6 doesn't do much except locate the file and print out the name, file size, and last time the file was modified. You can pass in different files on the command line to see what happens when a lookup fails. You should get a NameNotFoundException if you do. If you put the file foobar.txt into a directory under your jndi_root , you'll need to add the directory path to the argument on the command line. For example, if the file foobar.txt is in a directory called test under the jndi_root , you would need to run the program like this: java JNDILookupExample test/foobar.txt This example also uses the jndi.properties resource file. Make sure that you have the correct properties specified or it will not work. Your jndi.properties file should look like this for this example: java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory java.naming.provider.url=file:///c:/jndi_root |