import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
public class DNSLookup
{
public static void main(String args[])
{
String host = "www.google.com";
try
{
InetAddress inetAddress = InetAddress.getByName(host);
// show the Internet Address as name/address
System.out.println(inetAddress.getHostName() + " " + inetAddress.getHostAddress());
// get the default initial Directory Context
InitialDirContext iDirC = new InitialDirContext();
// get the DNS records for inetAddress
Attributes attributes = iDirC.getAttributes("dns://8.8.8.8/www.google.com", new String[] {"A"});
// get an enumeration of the attributes and print them out
NamingEnumeration<?> attributeEnumeration = attributes.getAll();
System.out.println("");
while (attributeEnumeration.hasMore())
{
System.out.println("" + attributeEnumeration.next());
}
attributeEnumeration.close();
}
catch (UnknownHostException exception)
{
System.err.println("ERROR: Cannot access '" + host + "'");
}
catch (NamingException exception)
{
System.err.println("ERROR: No DNS record for '" + host + "'");
exception.printStackTrace();
}
}
}
If I run this code, I get error like below...
www.google.com 74.125.128.103
ERROR: No DNS record for 'www.google.com'
javax.naming.CommunicationException: DNS error [Root exception is java.net.SocketTimeoutException: Receive timed out]; remaining name 'www.google.com'
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:300)
at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:430)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:231)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:139)
at com.sun.jndi.toolkit.url.GenericURLDirContext.getAttributes(GenericURLDirContext.java:103)
at javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:142)
at DNSLookup.main(DNSLookup.java:24)
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:121)
at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:145)
at java.net.DatagramSocket.receive(DatagramSocket.java:786)
at com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:411)
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:203)
... 7 more
But if I use "dns:/www.google.com" as query, not "dns://8.8.8.8/www.google.com", it works perfectly without any error. Error only happens when I try to specify DNS server to use.
"dns://8.8.8.8/www.google.com" -> ERROR
"dns:/www.google.com" -> Work!
http://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html
In this document, there is exact same usase what I want to use. I cannot understand why this makes problem.
DirContext ictx = new InitialDirContext();
Attributes attrs3 = ictx.getAttributes("dns://server1.example.com/host3.example.com",
new String[] {"MX"});
There is nothing wrong with your code, but I suspect that you're running it from behind a proxy or firewall that blocks outgoing DNS.
When you ask for dns:/www.google.com
you're essentially asking your local DHCP-supplied resolver for the IP of www.google.com
- just as when typing the address in the browser address line.
However, when using dns://8.8.8.8/www.google.com
you're asking to use Google DNS to resolve google.com
which requires that your proxy/firewall allows outgoing TCP/UDP port 53 traffic, which is usually not the case in corporate environments...
Cheers,