How do you test if the given hostname really exists in Java?
The problem is with some DNS services like Opendns which will return IP address even for non-existent DNS entries and, therefore, InetAddress.getByName( host) will always return something.
However, the host command is able to detect it somehow:
~$ host owqieyuqowiery.com
owqieyuqowiery.com has address 67.215.77.132
Host owqieyuqowiery.com not found: 3(NXDOMAIN)
Depends on what you mean by “really exists.” Do you mean that it’s a registered domain, with a website behind it? A host name is just a more easily readable/memorable form vs. an IP address for a given system. In other words, I could have a domain registered (which is just a publicly accessible short name for a system somewhere), and not host a website on a server that said domain points to, and that doesn’t make the domain any less “real”.
if you just want to lookup
Arecords, here’s a solution:If you want to know what
hostdoes, which might shed some light on why it does what it does, there’s some information on it here: http://linux.die.net/man/1/host Basically when this fails, it means the DNS lookup failed. That is, the DNS server(s) thathostconnected to in order to lookup that domain returned zero results.Also,
hostreturns more than just the DNSArecord (which is what is used for websites). It will also give you MX (mail server) records, etc.Ex: