title: Does JNDI use the DNS search list?
body: <p>I would like to use JNDI to look up Kerberos SRV records in a local network. I try to guess the local domain in hopefully clever ways. If that fails I would like to look up the plain entry, e.g. <code>_kerberos._tcp</code> without any suffix and rely on the DNS domain search list to find the right entry. This works on Windows with <code>nslookup -type=srv _kerberos._tcp</code> and Linux with <code>host -t srv _kerberos._tcp</code>. The domain <code>example.test</code> is appended and the entry is found.</p>
  
  <p>Here is an example program to do DNS lookups via JNDI:</p>
  
  <pre><code>import java.util.Hashtable;
  
  import javax.naming.Context;
  import javax.naming.NamingEnumeration;
  import javax.naming.NamingException;
  import javax.naming.directory.Attribute;
  import javax.naming.directory.Attributes;
  import javax.naming.directory.DirContext;
  import javax.naming.directory.InitialDirContext;
  
  public class JndiDnsTest {
  
      public static void main(String[] args) {
          if (args.length &lt; 2) {
              System.out.println("Usage: " + JndiDnsTest.class.getName() +
                      " name record-types...");
              return;
          }
          String name = args[0];
          String[] recordTypes = new String[args.length - 1];
          System.arraycopy(args, 1, recordTypes, 0, args.length - 1);
          Hashtable&lt;String, String&gt; env = new Hashtable&lt;String,String&gt;();
          env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
          try {
              DirContext ctx = new InitialDirContext(env);
              Attributes dnsQueryResult = ctx.getAttributes(name, recordTypes);
              if (dnsQueryResult == null) {
                  System.out.println("Not found: '" + name + "'");
              }
              for (String rrType: recordTypes) {
                  Attribute rr = dnsQueryResult.get(rrType);
                  if (rr != null) {
                      for (NamingEnumeration&lt;?&gt; vals = rr.getAll(); vals.hasMoreElements();) {
                          System.out.print(rrType + "\t");
                          System.out.println(vals.nextElement());
                      }
                  }
              }
          } catch (NamingException e) {
              e.printStackTrace(System.err);
          }
          System.out.println("\nThe DNS search list:");
          for (Object entry: sun.net.dns.ResolverConfiguration.open().searchlist()) {
              System.out.println(entry);
          }
          System.out.println("\nsun.net.spi.nameservice.domain = " +
                  System.getProperty("sun.net.spi.nameservice.domain"));
      }
  }
  </code></pre>
  
  <p>It appears to me that JNDI only does one lookup for the direct name. No entry is found where above commands succeed. It seems it does not use the DNS search list. Its contents are printed correctly at the bottom, though.</p>
  
  <p>On the other hand the <a href="http://download.oracle.com/javase/6/docs/technotes/guides/net/properties.html#jndi" rel="nofollow">Networking properties documentation</a> says that</p>
  
  <blockquote>
    <p>If the sun.net.spi.nameservice.domain property is not defined then the provider will use any domain or domain search list configured in the platform DNS configuration.</p>
  </blockquote>
  
  <p>(The property is not set.) The Java version is Sun Java 1.6.0_20.</p>
  
  <p><strong>Does JNDI use the DNS search list or not?</strong></p>