DNS resolution using JNDI

This is the kind of area where standing on the shoulders of JDK-giants pays dividends: the JNDI interface for looking up DNS records is surprisingly simple:

import java.util.Hashtable;
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;

class DNSLookup {

  private static DirContext provider;

   private static void setupProvider() {
       Hashtable env = new Hashtable();
       env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
       try {
         provider = new InitialDirContext(env);
       } catch (NamingException e) { System.out.println(e); }

   public static String[] resolve(String domain, String record) {
       if (provider == null) setupProvider();

       try {
           Attributes query = provider.getAttributes(domain, new String[] { record });
           Attribute records = query.get(record);
           NamingEnumeration recordData = records.getAll();
           int size = records.size();
           String[] data = new String[size];
           int i = 0;
           while (i < size) {
               data[i] = recordData.next().toString();
           return data;
       } catch (NamingException e) {
           return null;


Create a context, sling in a domain and record type, and off you go. Another reason why implementing rhinode has involved so little work on my part... I've not looked, but I suspect the original node.js implementation is a little longer than 40 LOC :)