Get started with the JNDI file system provider

The JNDI architecture allows you to access various naming and directory services, through a standard interface, mainly through the Context and DirContext interfaces.

Sun has written an implementation of these interfaces to access the filesystem: the filesystem service provider. It can be downloaded from

The following example shows you how to get a File object using the filesystem service provider. Make sure you have added the following JARs to your classpath:


Note: if you specify a directory, you’ll get an instance of a class that implements the Context interface, a JNDI subtree.

import javax.naming.*;
import java.util.*;
public class Main
   public static void main(String []args) {
      try {
         Properties properties = new Properties();
         properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
         properties.put(Context.PROVIDER_URL, "file:///");
         Context initialCtx = new InitialContext(properties);
         Object object = initialCtx.lookup("c:\autoexec.bat");
         if (object instanceof File) {
            System.out.println(((File) object).getName());
         else if (object instanceof Context) {
            System.out.println("You have specified a directory");
      catch(NamingException ne) {