1

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.

Prashant Shilimkar
  • 8,402
  • 13
  • 54
  • 89

1 Answers1

1

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());
    }
}}
Tom Mac
  • 9,693
  • 3
  • 25
  • 35
  • Thank you Tom for reply. I tried NS also, still it is showing same output. :( – Prashant Shilimkar Oct 24 '13 at 12:24
  • yes i have tried in both ways but same output...Have you tried it? – Prashant Shilimkar Oct 24 '13 at 12:56
  • 1
    I have - it works fine for me. Get the following console output: Nameserver ns1.google.com. Nameserver ns3.google.com. Nameserver ns4.google.com. Nameserver ns2.google.com. – Tom Mac Oct 24 '13 at 13:00
  • great. But not working for me :( I have tried it with javax.naming.directory but the code is not understandable by me, that is why i go with org.xbill.DNS lib. Have you ever use javax.naming.directory. Thanks for reply :) – Prashant Shilimkar Oct 24 '13 at 13:10