In the java code below, I am looking for DNS SRV records to resolve the name of the target domain and its associated port for the given domain name, such as root@1000000000.blubluzone.com. The search function specified in / HERE / below returns null somehow, and I cannot get the result of the query (i.e., the array of records is NULL). As a result, a null pointer exception is thrown when an array of records is accessed in a for loop.
What do you think I'm missing to make the following code. I am using dnsjava, and the corresponding jar API file is available at http://www.dnsjava.org/download/ (at the bottom of the page). Thank you in advance for your suggestion.
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 < 1) {
System.err.println("Usage: java -jar DNSLookup <hostname>");
System.exit(1);
}
String query = "_ssh._tcp." + args[0];
try {
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();
}
}
}
source