Not getting DNS record using org.xbill.DNS lib

3.2k Views Asked by At

I have written following code to get MX record for any Domain, here google.com

public class DNSRec {
public static void main(String... args) 
{
    try{
        Record [] records = new Lookup("http://www.google.com", Type.NS).run();
        for (int i = 0; i < records.length; i++) {
            NSRecord ns = (NSRecord) records[i];
            System.out.println("Nameserver " + ns.getTarget());
        }
    }catch(Exception e){
        System.out.println("Exception: "+e.getMessage());
    }
}}

Output: Exception: null

I have used org.xbill.DNS lib.

What is going wrong in above code ?

Should i use this library or there is any other better way to get DNS records?

Small example ;) most welcome :) . . . . Your response will be greatly appreciated

My internet connection is fine.

1

There are 1 best solutions below

4
On

Two things are wrong here:

  1. The code looks up MX records and then tries to cast the result to a NSRecord.
  2. You should not pass the protocol into the Lookup class constructor. You are doing a Nameserver lookup for a domain not a URL. Hence you should use google.com instead of http://www.google.com

Give this a go:

public class DNSRec {
public static void main(String... args) 
{
    try{
        Lookup lookup = new Lookup("google.com", Type.NS);
        Record[] records = lookup.run();

        for (int i = 0; i < records.length; i++) {
            NSRecord ns = (NSRecord) records[i];
            System.out.println("Nameserver " + ns.getTarget());
        }

    }catch(Exception e){
        System.out.println("Exception: "+e.getMessage());
    }
}}