0

I cant seem to figure out how to get this code to work properly. Am I doing something wrong? the program fails on the Record[] records = lookup.run(); line.

import java.util.Iterator;

    import org.xbill.DNS.ARecord;
    import org.xbill.DNS.Lookup;
    import org.xbill.DNS.Record;
    import org.xbill.DNS.TXTRecord;
    import org.xbill.DNS.TextParseException;
    import org.xbill.DNS.Type;
    Lookup lookup;
    try {
        lookup = new Lookup("google.com", Type.ANY);
        Record[] records = lookup.run();

        if (lookup.getResult() == Lookup.SUCCESSFUL) {
            String responseMessage = null;
            String listingType = null;
            for (int i = 0; i < records.length; i++) {
                if (records[i] instanceof TXTRecord) {
                    TXTRecord txt = (TXTRecord) records[i];

                    for (Iterator j = txt.getStrings().iterator(); j.hasNext();) {
                        responseMessage += (String) j.next();
                    }
                } else if (records[i] instanceof ARecord) {
                    listingType = ((ARecord) records[i]).getAddress()
                            .getHostAddress();
                }
            }
        }
    }catch (TextParseException e) {
        e.printStackTrace();
    }
1
  • 1
    If it fails by printing a stack trace, post the stack trace too. Commented Apr 21, 2011 at 16:03

1 Answer 1

1

What do you mean by it does not work? Please paste any exception trace as requested earlier. I was able to run the code just fine, I added output to the records so as to see the result. Here is the code:

import java.util.Iterator;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.TXTRecord;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;

public class DNS {

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

            if (lookup.getResult() == Lookup.SUCCESSFUL) {
                String responseMessage = null;
                String listingType = null;
                for (int i = 0; i < records.length; i++) {
                    if (records[i] instanceof TXTRecord) {
                        TXTRecord txt = (TXTRecord) records[i];
                        for (Iterator j = txt.getStrings().iterator(); j
                                .hasNext();) {
                            responseMessage += (String) j.next();
                        }
                        System.out.println("TXRecord " + responseMessage);
                    } else if (records[i] instanceof ARecord) {
                        listingType = ((ARecord) records[i]).getAddress()
                                .getHostAddress();
                        System.out.println("ARecord address : " + listingType);
                    }
                }
            }
        } catch (TextParseException e) {
            e.printStackTrace();
        }
    }
}

And here is the output:

ARecord address : 74.125.224.49
ARecord address : 74.125.224.51
ARecord address : 74.125.224.52
ARecord address : 74.125.224.48
ARecord address : 74.125.224.50
TXRecord nullv=spf1 include:_netblocks.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all
Sign up to request clarification or add additional context in comments.

5 Comments

if the same code does not work for OP but it works for you, it might be a network configuration issue on the OP's system. I imagine this function requires an internet connection, for a start.
I am running this inside of eclipse. The line Record[] records = lookup.run(); returns null. I have an internet connection so I know that is not the problem. Which version of dnsjava are you using could that be it or is it a network configuration issue? No Stack trace is printed it does not get past first if statement. If I comment out the first if statement then it throws a null pointer exception.
I am using dnsjava-2.0.0.jar. Another option is go to any other network and run the program from there (like home/work/starbucks/library) and see if it works.
Figured it out! I was using a corrupt dnsjava-2.0.0.jar. I am now using their smaller dnsjava-2.1.1.jar file and everything works great!
I have been having the same problem with the sample code above using the 2.1.1 library. Was there st else you did to solve the issue. Mine is still not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.