title: Java DNS Lookup for SRV records
body: <p>In the below java code, I am making a DNS SRV record lookup to resolve the target domain name and associated port for a given domain name such as root@1000000000.blubluzone.com. The lookup function indicated with /<em>HERE</em>/ below returns null somehow and I cannot get a a query result (i.e. record array is null). As a consequence, a null pointer exception is thrown when records array is accessed in the for loop. </p>
  
  <p>What do you think I am missing to make the following code work. I am using using dnsjava and the related API jar file is available at <a href="http://www.dnsjava.org/download/" rel="noreferrer">http://www.dnsjava.org/download/</a>  (at the bottom of the page). Thanks in advance for your suggestion.</p>
  
  <pre><code>     import org.xbill.DNS.Lookup;
       import org.xbill.DNS.Record;
       import org.xbill.DNS.SRVRecord;
       import org.xbill.DNS.TextParseException;
       import org.xbill.DNS.Type;
  
       public class DNSLookup {
  
          public static void main(String[] args) {
  
            if (args.length &lt; 1) {
               System.err.println("Usage: java -jar DNSLookup &lt;hostname&gt;");
               System.exit(1);
            }
  
            String query = "_ssh._tcp." + args[0];
  
            try {
              /*****HERE*********/
              Record[] records = new Lookup(query, Type.SRV).run();
  
              for (Record record : records) {
                   SRVRecord srv = (SRVRecord) record;
  
              String hostname = srv.getTarget().toString().replaceFirst("\\.$", "");
              int port = srv.getPort();
  
              System.out.println(hostname + ":" + port);
            }
            } catch (TextParseException e) {
               e.printStackTrace();
            }
         }
        }
  </code></pre>