95

I have the DNS server IP address and a hostname.

Using Java, how can I find the IP address of the hostname as returned by that DNS server using the IP address and the hostname?

Dark Matter
  • 2,231
  • 3
  • 17
  • 31
  • 4
    `dig ` in a command-line? – ceejayoz Apr 03 '13 at 13:12
  • How to do the same in java code and where should I use DNS-Server ip address and hostname. – Dark Matter Apr 03 '13 at 13:15
  • Another query will we need to provide the DNS Server ip address in any case or will it try to find it by default.I'm a bit confused here whether we need to use only the hostname or hostname+dns-server ip address too? – Dark Matter Apr 03 '13 at 13:16
  • 2
    The lookup of the address using the name is not complicated. As long as you just use the system's default resolver. So: do you really need to use the DNS server address? – A.H. Apr 03 '13 at 13:30
  • I have the same question : Do I reallly need DNS server address or will it resolve the address by default.BTW I am using a AIX Unix server. – Dark Matter Apr 03 '13 at 13:35

4 Answers4

143

Take a look at InetAddress and the getHostAddress() method.

InetAddress address = InetAddress.getByName("www.example.com"); 
System.out.println(address.getHostAddress()); 
thegrinner
  • 11,546
  • 5
  • 41
  • 64
  • Say if I have a dns ip address 10.37.221.152 and a host name say "www.student.com" then how can I find the ip address. – Dark Matter Apr 03 '13 at 13:31
  • 3
    @DarkMatter: `InetAddress` should get the DNS resolved address when you ask for `getHostAddress()`. I'm not sure if you can force it to use a *specific* DNS server though. – thegrinner Apr 03 '13 at 13:36
  • So it has to ideally resolve by itself without the programmer specifying the DNS server address explicitly right? – Dark Matter Apr 03 '13 at 13:37
  • 1
    @DarkMatter Yes. I believe it uses whatever DNS server is set on the computer itself. – thegrinner Apr 03 '13 at 13:39
34

You can do it like this:

for(InetAddress addr : InetAddress.getAllByName("stackoverflow.com"))
    System.out.println(addr.getHostAddress());
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
9

You can use InetAddress for this. Try the below code,

InetAddress address = InetAddress.getByName("www.yahoo.com");
System.out.println(address.getHostAddress());
System.out.println(address.getHostName());
Abhinaba Basu
  • 361
  • 1
  • 9
3

As suggested by all above, you can use InetAddress.getByName("hostName") but this can give you a cached IP, Read the java documentation for the same. If you want to get a IP from DNS you can use:

InetAddress[] ipAddress = DNSNameService.lookupAllHostAddr("hostName");
Gwenc37
  • 2,064
  • 7
  • 18
  • 22
Rudra
  • 411
  • 4
  • 7
  • For some reason this call gives me positive result for just any hostname. I thought I defaults to my IP, but no, it gives me some specific IP for any unknown host. – Imaskar Dec 21 '15 at 12:41
  • In addition to what @daksh pointed out, you can disable the DNS response cache as suggested [here](http://www.rgagnon.com/javadetails/java-0445.html) but be aware of the timing of disabling the cache as well as caching of DNS responses made by your OS as well as other DNS servers as suggested [here](https://stackoverflow.com/questions/1256556/any-way-to-make-java-honor-the-dns-caching-timeout-ttl). Hope this helps – CrazyGreenHand Aug 24 '14 at 10:37